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

时间:2015-02-15 05:41:08

标签: python menu syntax-error

我第一次学习Python。我有一个语法错误,我无法弄清楚我是否编码错误或者我是否有一个简单的拼写错误。我已将菜单拉出并放入一个新窗口,语法错误与我的菜单编码有关。我相信它就在编码显示菜单附近()。如果找到了答案,我会非常感谢解释为什么它不正确。 (编码中可能还有其他一些错误,但是我被困在菜单上。之后我会处理任何其他问题。建议虽然会受到赞赏。)

    #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()

2 个答案:

答案 0 :(得分:1)

你不能拥有一个包含空格的函数名称,用下划线_替换它,它应该可以工作。

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

1) 函数名称应为小写,单词用下划线分隔,而不是空格,以提高可读性。

2) 仅在已经成为主流风格的环境中允许使用mixedCase 变量...

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

    #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()

答案 1 :(得分:0)

第24行:您致电display menu()而不是display_menu()。此外,您需要在调用之前定义display_menu()