else:语句语法错误,简单的Python

时间:2015-10-06 09:59:29

标签: python if-statement

我的第二个问题:声明会抛出语法错误,不知道为什么?

这是用于检查sidewap数字的代码:

number1 = int(input("Enter a number between 0 and 9")) 
number2 = int(input("Enter a number between 0 and 9"))
number3 = int(input("Enter a number between 0 and 9"))


X = number1 + number2 + number3

while True:
        if X%3 !=0:
            print("Not a valid sideswap number")
            number1 = int(input("Enter a number between 0 and 9"))

            number2 = int(input("Enter a number between 0 and 9"))

            number3 = int(input("Enter a number between 0 and 9"))

        else:
            if number2 - number3 == 1:
                print("Not a valid sideswap number")
                number1 = int(input("Enter a number between 0 and 9"))

                number2 = int(input("Enter a number between 0 and 9"))

                number3 = int(input("Enter a number between 0 and 9"))

        else:           
            if number3 - number1 == 1:
                print("Not a valid sideswap number")
                number1 = int(input("Enter a number between 0 and 9"))

                number2 = int(input("Enter a number between 0 and 9"))

                number3 = int(input("Enter a number between 0 and 9"))

        else:
            if number1 - number2 == 1:
                print("Not a valid sideswap number")
                number1 = int(input("Enter a number between 0 and 9"))

                number2 = int(input("Enter a number between 0 and 9"))

                number3 = int(input("Enter a number between 0 and 9"))

3 个答案:

答案 0 :(得分:2)

而不是

else:
    if number2 - number3 == 1:

表达式是

elif number2 - number3 == 1:

查看documentation

>>> if x < 0:
...     x = 0
...     print 'Negative changed to zero'
... elif x == 0:
...     print 'Zero'
... elif x == 1:
...     print 'Single'
... else:
...     print 'More'

答案 1 :(得分:1)

python中没有像else if这样的东西。使用elif

例如:

else:           
    if number3 - number1 == 1:

将其更改为:

elif number3 - number1 == 1:

答案 2 :(得分:1)

对于同一else,您不能有多个if。您要找的是elif:。示例 -

while True:
        if X%3 !=0:
            print("Not a valid sideswap number")
            number1 = int(input("Enter a number between 0 and 9"))

            number2 = int(input("Enter a number between 0 and 9"))

            number3 = int(input("Enter a number between 0 and 9"))

        elif number2 - number3 == 1:
                print("Not a valid sideswap number")
                number1 = int(input("Enter a number between 0 and 9"))

                number2 = int(input("Enter a number between 0 and 9"))

                number3 = int(input("Enter a number between 0 and 9"))

        elif number3 - number1 == 1:
                print("Not a valid sideswap number")
                number1 = int(input("Enter a number between 0 and 9"))

                number2 = int(input("Enter a number between 0 and 9"))

                number3 = int(input("Enter a number between 0 and 9"))

        elif number1 - number2 == 1:
                print("Not a valid sideswap number")
                number1 = int(input("Enter a number between 0 and 9"))

                number2 = int(input("Enter a number between 0 and 9"))

                number3 = int(input("Enter a number between 0 and 9"))