【问题标题】:Why Am I Getting ELIF Invalid Syntax Error?为什么我会收到 ELIF 无效语法错误?
【发布时间】:2017-01-16 09:30:38
【问题描述】:

当我从 python 2.7 升级到 python 3.5 时,我开始遇到 ELIF 语句的问题。 我正在使用 PyCharm,所以当我输入 elif 语句时,它会显示错误并且 this1

this is what jumps up as error solution

当我按下它时会发生这种情况,但代码仍然无法正常工作......

不让我发这张照片,它会在 cmets 中

无论如何,由于某种原因,我无法发布代码,所以如果您需要他,它将在 cmets 中,如果可以,请帮助我,因为这不是第一次发生,我在任何地方都找不到帮助,这真的很烦人。 ..

【问题讨论】:

  • 你不能有空的elif 语句,它们需要在前面加上if。除此之外,您的if 应该使用相等运算符== 并且not 使用= 进行赋值。

标签: python-3.x pycharm python-3.5


【解决方案1】:

您的第一个错误是没有初始的if 语句以及game = '1': 而不是game == '1':。如果您查看我的代码,我已经修复了这些错误并修复了缩进,因为它导致了一些错误

import os

print("\nWelcome, enter Your name please")
name = input("--> ")

def username(name):         #NAME
    while len(name) < 2:
        print("There was an error")
        name = input("\nPlease enter Your name again -->")
    else:
        print("Hello,", name)
username(name)

def menu():         #MENU
    print("\nWelcome to the menu")
    print("From here You can chose the game")
    print("For now, we have only 3 game but there will be plenty more!")
    print("Chose game by it's number  ")
    print("1 - Coin flip| 2 - Horse racing| 3 - Loto|")
menu()
game = int(input("--> "))

def choice(game):           #CHOOSING GAME
    while game > 3 or game < 1:
        print("\nSomething went wrong, enter game you want again (only numbers 1, 2, 3!")
        game = int(input("--> "))
    if game == '1': #if statement first and two "=" signs
        print("You chose Coin flip game")
        os.system('python coinflip.py')
    elif game == '2': #use tab to indent properly
        print("You chose Horse racing game")
        os.system('python horseracing.py')
    elif game == '3': #keep indentations the same throughout
        print("You chose Loto game")
        os.system("python loto.py")
choice(game)

【讨论】:

    【解决方案2】:

    您需要先输入“if”和“elif”。所以应该是这样的:

    def choice(game):           #CHOOSING GAME
    while game > 3 or game < 1:
        print("\nSomething went wrong, enter game you want again (only numbers 1, 2, 3!")
        game = int(input("--> "))
        if game == '1': #bug here
            print("You chose Coin flip game")
            os.system('python coinflip.py')
        elif game == '2': #and here
           print("You chose Horse racing game")
            os.system('python horseracing.py')
        elif game == '3': #and here
            print("You chose Loto game")
            os.system("python loto.py")
    

    【讨论】:

    • 哦,我明白了,非常感谢!这对我帮助很大!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 2022-01-23
    • 2014-11-23
    相关资源
    最近更新 更多