0

在你问之前,我确实看过其他答案和问题,它们似乎都是一样的,是的,我确实添加了它,但仍然没有。好的,所以我有一个带有菜单的程序,并且在菜单选项中它不断在“ ”冒号处出现语法错误,请帮助我已经尝试了一切。这是代码:

def main():
    print("Hello and Welcome to the 'Say When' program")

    print("1:Class\n2:Priamry\n3:Secondary\n4:FirstGag\n5:SecondGag")
    menu = (input("What would you like to search?: ")
            if menu == '1':
                print("You chose Class")
                list = ['Assault', 'Engineer', 'Support', 'Recon']
                from random import choice
                print(choice(list))

            elif menu == '2'
                print("nice")

错误 =语法错误,然后它以红色突出显示“ ”。

4

2 回答 2

1

您的input函数缺少右括号,您根本不需要。

menu = input("What would you like to search?: ")

完整的固定代码:

def main():
    print("Hello and Welcome to the 'Say When' program")

    print("1:Class\n2:Priamry\n3:Secondary\n4:FirstGag\n5:SecondGag")
    menu = input("What would you like to search?: ")
    if menu == '1':
        print("You chose Class")
        list = ['Assault', 'Engineer', 'Support', 'Recon']
        from random import choice
        print(choice(list))

    elif menu == '2':
        print("nice")
于 2013-11-10T16:43:18.190 回答
1
  1. 开头有一个不必要的input()括号。
  2. if语句的缩进错误更多。
  3. elif没有_:
  4. 必须在开始时进行导入。

正确的代码

from random import choice
def main():
    print("Hello and Welcome to the 'Say When' program")

    print("1:Class\n2:Priamry\n3:Secondary\n4:FirstGag\n5:SecondGag")
    menu = input("What would you like to search?: ")
    if menu == '1':
        print("You chose Class")
        list = ['Assault', 'Engineer', 'Support', 'Recon']
        print(choice(list))

    elif menu == '2':
        print("nice")
于 2013-11-10T16:44:09.917 回答