Python用户名、密码if-else嵌套问题
Python username, password if-else nesting problem
我正在学习 python 并且有一个任务要解决。
在任务中,代码应如下所示:
if [selection]:
[code]
if [selection]:
[code]
else:
[code}
else:
[code]
输出应该是:
Give name: Peter
The given name is wrong.
或
Give name: John
Give password: Monkeys?
The password is incorrect.
或
Give name: John
Give password: ABC123
Both inputs are correct!
到目前为止我想到的最好的是:
name = input("Give name:")
password = input("Give password:")
if name == "John":
if password == "ABC123":
print("Both inputs are correct!")
else:
print("The given password is incorrect.")
else:
print("The given name is wrong.")
但到目前为止,当我输入正确的数据时,一切正常,但如果我输入错误的名称,它不会告诉我名称错误,而是立即要求输入密码,然后告诉我名称是错误的。它应该立即告诉我名字是错误的,如果正确然后继续询问密码。到目前为止,教程并没有真正帮助我。
你应该多花一点时间了解 python 代码是如何运行的,顺便说一句
name = input("Give name:")
if name == "John":
password = input("Give password:")
if password == "ABC123":
print("Both inputs are correct!")
else:
print("The given password is incorrect.")
else:
print("The given name is wrong.")
我正在学习 python 并且有一个任务要解决。 在任务中,代码应如下所示:
if [selection]:
[code]
if [selection]:
[code]
else:
[code}
else:
[code]
输出应该是:
Give name: Peter
The given name is wrong.
或
Give name: John
Give password: Monkeys?
The password is incorrect.
或
Give name: John
Give password: ABC123
Both inputs are correct!
到目前为止我想到的最好的是:
name = input("Give name:")
password = input("Give password:")
if name == "John":
if password == "ABC123":
print("Both inputs are correct!")
else:
print("The given password is incorrect.")
else:
print("The given name is wrong.")
但到目前为止,当我输入正确的数据时,一切正常,但如果我输入错误的名称,它不会告诉我名称错误,而是立即要求输入密码,然后告诉我名称是错误的。它应该立即告诉我名字是错误的,如果正确然后继续询问密码。到目前为止,教程并没有真正帮助我。
你应该多花一点时间了解 python 代码是如何运行的,顺便说一句
name = input("Give name:")
if name == "John":
password = input("Give password:")
if password == "ABC123":
print("Both inputs are correct!")
else:
print("The given password is incorrect.")
else:
print("The given name is wrong.")