首页 > 解决方案 > 为什么 else 在这个简单的代码中显示为语法错误?

问题描述

所以这是我的代码,我试图这样做,以便如果用户名或密码错误,它会立即说出哪个错误并且没有通过,但我无法弄清楚为什么 ELSE 显示为语法错误:(编辑:不知何故第一行没有出现)

username = input("Write the username:")
password = input("Write the password:")
if username == "Toby":
    pass
else:
    print("The username was wrong.")
if password == "Traveler":
    pass
else:
    print("The password was wrong.")
print("Password and username were correct!")

标签: pythonpython-3.x

解决方案


从您的代码片段来看,您的缩进似乎没有对齐。

确保使用 4 个空格来告诉解释器您现在处于 if/else 语句中。

if condition:
    if_logic()
else:
    else_logic()

推荐阅读