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

标签 python menu syntax-error

我是第一次学习Python。我有语法错误,我无法弄清楚编码是否错误或是否有简单的错字。我已经将菜单拉出并放到新窗口中,语法错误与菜单编码有关。我相信它在编码显示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()

最佳答案

您不能使用包含空格的函数名,而应使用下划线_替换它,它应该可以工作。

参见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()

关于python - 显示菜单的python程序中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28523375/

相关文章:

java - 使用鼠标而不是键盘选择按钮

php - MYSQL语句搞不明白是怎么回事

python - 在 Pandas 中创建列联表

java - 有多少数据可以存储到 Google App Engine 应用程序中?

javascript - 关闭时滑动菜单堆叠文本

python - 自定义python函数不起作用

C# MySQL 语法错误

python - 在 Matplotlib 中设置绘图 Canvas 的大小

python - 需要处理具有非唯一多索引的串联数据帧

jquery - li 菜单选项卡以填充页面宽度(平均)