my python编程的语法有一些语法错误
这是练习希望输出的方式:my python编程的语法有一些语法错误,python,if-statement,Python,If Statement,这是练习希望输出的方式: 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! 这是我写的代码: print("Give name:"); name = input(); if name == "John
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!
这是我写的代码:
print("Give name:");
name = input();
if name == "John":
print(name);
else:
print("The given name is wrong.");
print("Give password:");
password = input();
if password != "ABC123":
print("The password is incorrect.");
else:
print("Both inputs are correct!");
但控制台会打印出以下内容:
Give name:
Peter
Traceback (most recent call last):
File "ohjelma.py", line 9, in
password = input();
The given name is wrong.
Give password:
EOFError: EOF when reading a line
这是因为无论您的第一条
if
语句是否正确,您的程序都将继续运行。您可以翻转第一个if
条件,然后将密码验证嵌套在第二个条件中:
print("Give name:")
name = input()
if name != "John": # This prevents your program from proceeding to the password part
print("The given name is wrong.")
else:
print(name)
print("Give password:")
password = input()
if password != "ABC123":
print("The password is incorrect.")
else:
print("Both inputs are correct!")
另外,你并不需要
在python中,换行符和制表符工作得很好,实际上是python中建议的编码方式您如何向程序提供输入?EOFError只能来自非控制台输入。