現在你的腳本只檢查用戶名是否「student1」和執行上的密碼無用的檢查。試試這個版本(假設的Python 2.7):
username = raw_input("please enter your username")
password = raw_input("please enter your password")
if username == "student1" and password == "password123":
print "access granted"
else:
print "incorrect login"
更妙的是,你應該哈希密碼,因爲現在它足以打開Python文件,並環顧四周,找到正確的密碼。舉個例子:
from hashlib import md5
username = raw_input("please enter your username")
password = raw_input("please enter your password")
password2 = md5()
password2.update(password)
if username == "student1" and password2.hexdigest() == "482c811da5d5b4bc6d497ffa98491e38":
print "access granted"
else:
print "incorrect login"
我生成這個代碼的哈希:
from hashlib import md5
m = md5()
m.update('password123')
print m.hexdigest()
來源
2017-08-24 16:08:48
GRB
你能更詳細一點的「它不工作」?究竟是什麼問題? – Mureinik
閱讀[邏輯運算符](https://www.programiz.com/python-programming/operators#logical_operators)。另外,你的身份是錯誤的。縮進在Python中很重要 – litelite
只需添加。不要按原樣存儲密碼。加密它們(使用sha1 +鹽等)。 – akp