python - 为什么 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!")
解决方案
从您的代码片段来看,您的缩进似乎没有对齐。
确保使用 4 个空格来告诉解释器您现在处于 if/else 语句中。
if condition:
if_logic()
else:
else_logic()
推荐阅读
- java - Java:在线程内运行时 JNDI 查找失败
- swift - 无法检查查询结果是否为零
- angular - 将primeng toast放在角度6的共享组件上时,第一次单击时未打开
- php - Base64解码在php中没有给出值
- python - Visual Studio 2015中python字符串的f格式
- arduino - 连续两次调用analogWrite 后,Arduino Mega2560 重新启动
- linux - Anaconda Navigator 无法在 Linux Mint 上启动
- sendgrid - 如何将文件附加到 Sendgrid 单次发送电子邮件?
- python - 训练中的损失始终为零
- c# - 如何根据 C# 中的条件组合具有不同属性的两个列表