适用于绝对初学者的Python编程,第3章示例无效

时间:2018-12-07 10:20:46

标签: python python-3.x

我不确定在这里做什么...正在阅读此示例,但是即使我复制/粘贴了代码,它也对我不起作用。 它所做的只是打印我最后的“客人”文本。

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.")

3 个答案:

答案 0 :(得分:1)

" "传递为True,因此应将用户名和密码设置为""

答案 1 :(得分:1)

usernamepassword都是' ',如果将它们替换为''的值,它将起作用,或者这些行:

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)

  1. 您可以这样声明用户名和密码:username = ""password = ""

  2. 您只需将用户名和密码设置为None。由于python是一种动态编程语言,因此在通过控制台输入用户名时,它将自动调整String。

通过控制台输入字符串时,最好添加一个strip ()函数。类似于:username = input("Username:").strip ()。 这样可以防止在字符串前后出现意外空格。