我不确定在这里做什么...正在阅读此示例,但是即使我复制/粘贴了代码,它也对我不起作用。 它所做的只是打印我最后的“客人”文本。
security = 0
username = " "
while not username:
username = input("Username:")
password = " "
while not password:
password = input("Password:")
if username == "M.Dawson" and password == "secret":
print("Hi, Mike.")
security = 5
elif username == "S.Meier" and password == "civilization":
print("Hey, Sid.")
security = 3
elif username == "S.Miyamoto" and password == "mariobros":
print("What's up, Shigeru?")
security = 3
elif username == "W.Wright" and password == "thesims":
print("How goes it, Will?")
security = 3
elif username == "guest" or password == "guest":
print("Welcome, guest.")
security = 1
else:
print("Login failed. You're not so exclusive.")
答案 0 :(得分:1)
" "
传递为True,因此应将用户名和密码设置为""
。
答案 1 :(得分:1)
username
和password
都是' '
,如果将它们替换为''
的值,它将起作用,或者这些行:
username = " "
while not username:
username = input("Username:")
password = " "
while not password:
password = input("Password:")
必须是:
username = " "
while username==" ":
username = input("Username:")
password = " "
while password==" ":
password = input("Password:")
或者必须是:
username = " "
while username.isspace():
username = input("Username:")
password = " "
while password.isspace():
password = input("Password:")
答案 2 :(得分:0)
您可以这样声明用户名和密码:username = ""
,password = ""
或
您只需将用户名和密码设置为None
。由于python是一种动态编程语言,因此在通过控制台输入用户名时,它将自动调整String。
通过控制台输入字符串时,最好添加一个strip ()
函数。类似于:username = input("Username:").strip ()
。
这样可以防止在字符串前后出现意外空格。