What is the reason I am getting a "NameError: name 'int_to_roman' is not defined"? How do I resolve this?

What is the reason I am getting a "NameError: name 'int_to_roman' is not defined"? How do I resolve this?

我试图向这个基本的罗马数字计算器添加一个 class,但是当我尝试 运行 来自 class 的函数时,我得到一个 NameError。我不确定发生了什么。

我试图改变代码的顺序,但没有别的,因为我不知道错误的原因是什么。

此外,我不知道为什么,但我无法将 class(下方)部分放入代码块中。

class Roman_Number():
    roman_numeral_table = [
    ("M", 1000), ("CM", 900), ("D", 500),
    ("CD", 400), ("C", 100),  ("XC", 90),
    ("L", 50),   ("XL", 40),  ("X", 10),
    ("IX", 9),   ("V", 5),    ("IV", 4),
    ("I", 1)
    ]


    r = input('If you want to go from Roman to Number, enter "1." If you want to go from Number to Roman, enter "2"')

    if r == 1:
        roman_to_int()
    else:
        int_to_roman()

    def int_to_roman():
        number = int(input('Provide Number: '))
        if number < 1 or number > 3999:
            print('Number must be inbetween 1 and 3999')
        else:
            print('Valid Number')

        roman_numerals = []
        for numeral, value in roman_numeral_table:
            while value <= number:
                number -= value
                roman_numerals.append(numeral)

        print(''.join(roman_numerals))

        def roman_to_int():
            pass

我希望它开始 运行ning int_to roman(),但我只是收到一个错误。

我更改了订单,现在收到 "roman_numeral_table is not defined." 为什么以及如何解决这个问题?

您收到此错误,因为 python 按照语法顺序执行块,您需要先定义一个函数,然后才能调用它/引用它。试试这个:

def roman_to_int():
      pass

def int_to_roman():
      number = int(input('Provide Number: '))
      if number < 1 or number > 3999:
          print('Number must be inbetween 1 and 3999')
      else:
          print('Valid Number')


class Roman_Number():
    roman_numeral_table = [
    ("M", 1000), ("CM", 900), ("D", 500),
    ("CD", 400), ("C", 100),  ("XC", 90),
    ("L", 50),   ("XL", 40),  ("X", 10),
    ("IX", 9),   ("V", 5),    ("IV", 4),
    ("I", 1)
    ]

    r = input('If you want to go from Roman to Number, enter "1." If you want to go from Number to Roman, enter "2"')

    if r == 1:
        roman_to_int()
    else:
        int_to_roman()

        roman_numerals = []
        for numeral, value in roman_numeral_table:
            while value <= number:
                number -= value
                roman_numerals.append(numeral)

        print(''.join(roman_numerals))

Python 是一种解释型语言。因此,需要在第一次调用之前定义该函数。只需将 def int_to_roman(): 块移动到 roman_numeral_table 定义的正下方,代码就可以正常工作。

像这样:

class Roman_Number():
    roman_numeral_table = [
    ("M", 1000), ("CM", 900), ("D", 500),
    ("CD", 400), ("C", 100),  ("XC", 90),
    ("L", 50),   ("XL", 40),  ("X", 10),
    ("IX", 9),   ("V", 5),    ("IV", 4),
    ("I", 1)
    ]

    def int_to_roman():
        number = int(input('Provide Number: '))
        if number < 1 or number > 3999:
            print('Number must be inbetween 1 and 3999')
        else:
            print('Valid Number')

        roman_numerals = []
        for numeral, value in roman_numeral_table:
            while value <= number:
                number -= value
                roman_numerals.append(numeral)

        print(''.join(roman_numerals))

        def roman_to_int():
            pass


    r = input('If you want to go from Roman to Number, enter "1." If you want to go from Number to Roman, enter "2"')

    if r == 1:
        roman_to_int()
    else:
        int_to_roman()

将您的代码更改为类似这样的代码,它应该可以工作。 Python 要求您在调用函数之前定义它。

    class Roman_Number():
        roman_numeral_table = [
        ("M", 1000), ("CM", 900), ("D", 500),
        ("CD", 400), ("C", 100),  ("XC", 90),
        ("L", 50),   ("XL", 40),  ("X", 10),
        ("IX", 9),   ("V", 5),    ("IV", 4),
        ("I", 1)
        ]


        r = input('If you want to go from Roman to Number, enter "1." If you want to go from Number to Roman, enter "2"')
        def int_to_roman():
            number = int(input('Provide Number: '))
            if number < 1 or number > 3999:
                print('Number must be inbetween 1 and 3999')
            else:
                print('Valid Number')

            roman_numerals = []
            for numeral, value in roman_numeral_table:
                while value <= number:
                    number -= value
                    roman_numerals.append(numeral)

            print(''.join(roman_numerals))

            def roman_to_int():
                pass

        if r == 1:
            roman_to_int()
        else:
            int_to_roman()