带有显示菜单的 python 程序中的语法错误

Syntax Error in in a python program with a displayed menu

我是第一次学习Python。我有一个语法错误,我不知道是我编码错误还是我只是有一个简单的拼写错误。我已将菜单拉出并放入新的 window 中,语法错误与我对菜单的编码有关。我相信它在编码显示 menu() 附近的某个地方。如果找到答案,我将不胜感激解释为什么它不正确。 (编码中可能还有其他一些错误,但我卡在菜单上了。之后我会处理出现的任何其他问题。尽管提出建议,我们将不胜感激。)

    #This program allow for the user to convert weight managements

#Import Modules
import KilogramConversion
import PoundConversion
import TonConversion

    #Menu choice constants

CONVERT_KILOGRAMS_OUNCE=1
CONVERT_KILOGRAMS_POUND=2
CONVERT_POUND_OUNCE=3
CONVERT_POUND_TON=4
CONVERT_TON_POUND=5
CONVERT_TON_OUNCE=6
CONVERT_TON_KILOGRAMS=7
QUIT_CHOICE=8

    # Main Function
def main():
    choice=0

while choice != QUIT_CHOICE:
    display menu()
    choice=int(input('Enter your choice:'))

    #Perform menu choice
    if choice == CONVERT_KILOGRAMS_OUNCE:
        kilo=float(input("Enter weight in Kilograms:"))
        print('The weight in ounces is:', KilogramConversion.ounce(kilo))
    elif choice == CONVERT_KILOGRAMS_POUND:
        kilo=float(input("Enter weight in Kilograms:"))
        print('The weight in pounds is:', KilogramConversion.pound(kilo))
    elif choice == CONVERT_POUND_OUNCE:
        lbs=float(input("Enter weight in Pounds:"))
        print('The weight in ounces is:', PoundConversion.ounce(lbs))
    elif choice == CONVERT_POUND_TON:
        lbs=float(input("Enter weight in Pounds:"))
        print('The weight in tons is:', PoundConversion.ton(lbs))
    elif choice == CONVERT_TON_POUND:
        tons= float(input("Enter weight in Tons:"))
        print('The weight in pounds is:', TonConversion.pound(tons))
    elif choice == CONVERT_TON_OUNCE:
        tons= float(input("Enter weight in Tons:"))
        print('The weight in ounces is:', TonConversion.ounce(tons))
    elif choice == CONVERT_TON_KILOGRAMS:
        tons= float(input("Enter weight in Tons:"))
        print('The weight in kilograms is:', TonConversion.kilogram(tons))
    elif choice == QUIT_CHOICE:
        print('Exiting...')
    else:
        print('Invalid Selection')

def display menu():
    print('          ')
    print('          ')
    print('          Menu')
    print('1. Kilograms to Ounce')
    print('2. Kilograms to Pound')
    print('3. Pound to Ounce')
    print('4. Pound to Ton')
    print('5. Ton to Pound')
    print('6. Ton to Ounce')
    print('7. Ton to Kilograms')
    print('8. Quit')

main()

第 24 行:您调用了 display menu() 而不是 display_menu()。另外,你需要在调用它之前定义display_menu()

你不能有一个包含 space 的函数名称,将其替换为下划线 _ ,它应该可以工作。

参见 Python PEP 8。必须严格遵守命名变量的规则。

1) 函数名应该是小写的, 个单词用下划线分隔,而不是 space s 根据需要提高可读性。

2) 混合大小写仅在已经是主流风格的上下文中才允许 变量...

因此您更新后的代码将如下所示:

    #This program allow for the user to convert weight MANAGEMENTS

#Import Modules
import KilogramConversion
import PoundConversion
import TonConversion

    #Menu choice constants

CONVERT_KILOGRAMS_OUNCE=1
CONVERT_KILOGRAMS_POUND=2
CONVERT_POUND_OUNCE=3
CONVERT_POUND_TON=4
CONVERT_TON_POUND=5
CONVERT_TON_OUNCE=6
CONVERT_TON_KILOGRAMS=7
QUIT_CHOICE=8

    # Main Function
def main():
    choice=0

    while choice != QUIT_CHOICE:
       display_menu()
       choice=int(input('Enter your choice:'))

       #Perform menu choice
       if choice == CONVERT_KILOGRAMS_OUNCE:
          kilo=float(input("Enter weight in Kilograms:"))
          print('The weight in ounces is:', KilogramConversion.ounce(kilo))
       elif choice == CONVERT_KILOGRAMS_POUND:
          kilo=float(input("Enter weight in Kilograms:"))
          print('The weight in pounds is:', KilogramConversion.pound(kilo))
       elif choice == CONVERT_POUND_OUNCE:
          lbs=float(input("Enter weight in Pounds:"))
          print('The weight in ounces is:', PoundConversion.ounce(lbs))
       elif choice == CONVERT_POUND_TON:
          lbs=float(input("Enter weight in Pounds:"))
          print('The weight in tons is:', PoundConversion.ton(lbs))
       elif choice == CONVERT_TON_POUND:
          tons= float(input("Enter weight in Tons:"))
          print('The weight in pounds is:', TonConversion.pound(tons))
       elif choice == CONVERT_TON_OUNCE:
          tons= float(input("Enter weight in Tons:"))
          print('The weight in ounces is:', TonConversion.ounce(tons))
       elif choice == CONVERT_TON_KILOGRAMS:
          tons= float(input("Enter weight in Tons:"))
          print('The weight in kilograms is:', TonConversion.kilogram(tons))
       elif choice == QUIT_CHOICE:
           print('Exiting...')
       else:
           print('Invalid Selection')

def display_menu():
    print('          ')
    print('          ')
    print('          Menu')
    print('1. Kilograms to Ounce')
    print('2. Kilograms to Pound')
    print('3. Pound to Ounce')
    print('4. Pound to Ton')
    print('5. Ton to Pound')
    print('6. Ton to Ounce')
    print('7. Ton to Kilograms')
    print('8. Quit')

main()