在 Python 上登录系统
Login system on Python
这真是一个新手问题。
所以,我试图在 Python 中编写一个登录系统,它要求输入一个用户名(只有 1 个可用的用户名),如果输入的用户名不正确,它说用户名无效,如果是正确的,它要求输入密码,如果密码不正确,它会说密码不正确,然后再次询问密码,如果输入的密码正确,它只会说已登录。
到目前为止我能做的是:
a = 0
while a < 1:
print ('Username:')
name = input()
if name != 'Teodor': #checks the username typed in
print ('Invalid username.')
continue
else:
print ('Hello, Teodor.')
print ('Password:')
a = a + 1
password = input()
b = 0
while b < 1:
if password != '1234': #checks the password typed in
print ('Password incorrect.')
b = b + 1
continue
else:
print ('Password correct.')
print ('Logging in...')
print ('Logged in.')
break
这行得通,但如果用户输入的密码不正确,它会做一些我不想做的事情。如果用户输入的密码不正确,我希望程序告诉用户 'Incorrect password' 并再次询问它,但它并没有这样做,它只是打印 'Incorrect password',然后它终止。不过,它在要求用户名的部分 100% 有效。
这是我想念的一件小事。我怎样才能解决这个问题?
非常感谢!
检查密码时不需要+ 1
。那只会让你脱离循环。
相反,尝试:
if password != '1234': #checks the password typed in
print ('Password incorrect.')
continue
一个更好的解决方案是使用布尔值,而不是使用 +1
和 <1
来跳出循环。
示例:
userCorrect = False
while not userCorrect:
print ('Username:')
name = raw_input()
if name != 'Teodor': #checks the username typed in
print ('Invalid username.')
continue
else:
print ('Hello, Teodor.')
print ('Password:')
userCorrect = True
password = raw_input()
passCorrect = False
while not passCorrect:
if password != '1234': #checks the password typed in
print ('Password incorrect.')
print ('Password:')
password = raw_input()
else:
passCorrect = True
# Since password is correct, continue.
print ('Password correct.')
print ('Logging in...')
print ('Logged in.')
此循环 (while b < 1:
) 在输入无效密码时终止。
看看
> if password != '1234': #checks the password typed in
> print ('Password incorrect.')
> b = b + 1
> continue
代码行 b = b + 1
使得 while b < 1:
变为假,从而结束循环并终止您的程序。
语句 b = b + 1
会在用户每次输入错误密码时终止您的 while
循环。真的没必要。
您也可以将密码提示包装在 while
循环中:
while input("Enter password") != "1234":
print("Password incorrect")
正如其他人已经指出的那样,问题在于 b = b + 1
打破了条件 while b < 1:
,导致它不再要求输入另一个密码。简单删除行 b = b + 1
想让它变得更好吗?
用 getpass()
代替 input()
避免 'over-the-shoulder' 攻击。您的密码输入被屏蔽为 ****
例如
from getpass import getpass
password = getpass()
加密
好吧,除了听起来很酷之外,这并不能真正阻止某些人修改代码以跳过密码阶段——但它可以阻止他们在代码中看到原始密码。
this post 有一个很好的例子使用 passlib
这有助于保护 non-unique/sensitive 密码(例如您用于 5 倍其他内容的密码,或者您母亲的娘家姓...不要将她拖入其中)
这真是一个新手问题。 所以,我试图在 Python 中编写一个登录系统,它要求输入一个用户名(只有 1 个可用的用户名),如果输入的用户名不正确,它说用户名无效,如果是正确的,它要求输入密码,如果密码不正确,它会说密码不正确,然后再次询问密码,如果输入的密码正确,它只会说已登录。
到目前为止我能做的是:
a = 0
while a < 1:
print ('Username:')
name = input()
if name != 'Teodor': #checks the username typed in
print ('Invalid username.')
continue
else:
print ('Hello, Teodor.')
print ('Password:')
a = a + 1
password = input()
b = 0
while b < 1:
if password != '1234': #checks the password typed in
print ('Password incorrect.')
b = b + 1
continue
else:
print ('Password correct.')
print ('Logging in...')
print ('Logged in.')
break
这行得通,但如果用户输入的密码不正确,它会做一些我不想做的事情。如果用户输入的密码不正确,我希望程序告诉用户 'Incorrect password' 并再次询问它,但它并没有这样做,它只是打印 'Incorrect password',然后它终止。不过,它在要求用户名的部分 100% 有效。
这是我想念的一件小事。我怎样才能解决这个问题? 非常感谢!
检查密码时不需要+ 1
。那只会让你脱离循环。
相反,尝试:
if password != '1234': #checks the password typed in
print ('Password incorrect.')
continue
一个更好的解决方案是使用布尔值,而不是使用 +1
和 <1
来跳出循环。
示例:
userCorrect = False
while not userCorrect:
print ('Username:')
name = raw_input()
if name != 'Teodor': #checks the username typed in
print ('Invalid username.')
continue
else:
print ('Hello, Teodor.')
print ('Password:')
userCorrect = True
password = raw_input()
passCorrect = False
while not passCorrect:
if password != '1234': #checks the password typed in
print ('Password incorrect.')
print ('Password:')
password = raw_input()
else:
passCorrect = True
# Since password is correct, continue.
print ('Password correct.')
print ('Logging in...')
print ('Logged in.')
此循环 (while b < 1:
) 在输入无效密码时终止。
看看
> if password != '1234': #checks the password typed in
> print ('Password incorrect.')
> b = b + 1
> continue
代码行 b = b + 1
使得 while b < 1:
变为假,从而结束循环并终止您的程序。
语句 b = b + 1
会在用户每次输入错误密码时终止您的 while
循环。真的没必要。
您也可以将密码提示包装在 while
循环中:
while input("Enter password") != "1234":
print("Password incorrect")
正如其他人已经指出的那样,问题在于 b = b + 1
打破了条件 while b < 1:
,导致它不再要求输入另一个密码。简单删除行 b = b + 1
想让它变得更好吗?
用 getpass()
代替 input()
避免 'over-the-shoulder' 攻击。您的密码输入被屏蔽为 ****
例如
from getpass import getpass
password = getpass()
加密
好吧,除了听起来很酷之外,这并不能真正阻止某些人修改代码以跳过密码阶段——但它可以阻止他们在代码中看到原始密码。
this post 有一个很好的例子使用 passlib
这有助于保护 non-unique/sensitive 密码(例如您用于 5 倍其他内容的密码,或者您母亲的娘家姓...不要将她拖入其中)