在Python中使用else函数时出现错误

时间:2019-03-03 04:56:31

标签: python

x = 30
y = 60

if (x>y):
    print("True")

    else:
        print("False")

错误

 File "<ipython-input-1-05aca812b623>", line 7
    else:
       ^
SyntaxError: invalid syntax

4 个答案:

答案 0 :(得分:0)

else必须与if垂直对齐。

答案 1 :(得分:0)

我认为问题是缩进 请尝试以下。

x = 30 
y = 60

if (x>y):
    print("True")

else:
    print("False")

答案 2 :(得分:0)

此代码运行正常。

while True:
    userResponse = str(input("Please enter name of guest: "))
    if userResponse == 'quit':
        for index, userResponse in enumerate(list):
            print(f'Guest {index + 1} is {userResponse}')
        userDelete = input('Do you want to delete any users? If so type their corresponding number to delete or type "quit" to print list and end program.')
        if userDelete == 'quit':
            for index, userResponse in enumerate(list):
                print(f'Guest {index + 1} is {userResponse}')
            break
        elif userDelete in list:
            list.pop(userResponse)
            for index, userResponse in enumerate(list):
                print(f'Guest {index + 1} is {userResponse}')
            break
        elif userDelete not in list:
            print('You tried deleting a name that is not in the list, please try again.')
            userDelete
            list.pop(userResponse)
    elif userResponse not in list:
        list.append(userResponse)
    elif userResponse in list:
        print('Name already in list, try another or enter "quit" to show list of guests.')
        userResponse

缩进在python中非常重要。确保您正确缩进代码。

答案 3 :(得分:0)

缩进在python中很重要。正确缩进您的代码,它将正常工作。

x = 30 
y = 60
if (x>y): 
    print("True")
else:
    print("False")