在任务中写道,代码应该如下所示:
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.")
但到目前为止,当我输入正确的数据时,一切正常。但如果我输入了错误的名称,它不会告诉我该名称是错误的,而是立即要求我输入密码,然后告诉我该名称是错误的。
它应该立即告诉我名称错误,如果名称正确,则继续询问密码。
外部
if
应检查 name
,然后内部 if
应检查密码。
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.")
A = something
if A == "something":
print ("welcome")
else:
print("wrong statement")