【问题标题】:Error with username/password checker用户名/密码检查器出错
【发布时间】:2017-10-07 20:15:46
【问题描述】:

我的任务是设计一个 Python 程序,它允许三个用户(Alice、Bob 和 Charles)之一输入所需的密码。我的程序将确定它是否是有效密码。此外,我的程序还应该检查该用户过去是否已经使用过此密码,如果他们以前使用过,则返回错误消息。

我已经成功编写了用户名和密码条件,但是当用户输入或尚未输入密码时,我无法让代码返回无效/有效消息。

user = input("Which user is this? ")
if user == "Alice" or user == "Bob" or user == "Charles":
    print("Welcome, ",user,"!",sep="")
else:
    print("I don't recognize that username. Please try again.")

def checkPassword(pw):
    errorMessage = ''

if ((len(pw) > 20)):
    errorMessage += "--Password is too long. 20 characters is the max.\n"
elif (len(pw) < 12):
    errorMessage += "--Password is too short. 12 characters is the min.\n"

if pw.isalpha():
    errorMessage += "--Password needs at least one digit.\n"

if pw.isdigit():
    errorMessage += "--Password needs at least one letter.\n"

if pw.isupper():
    errorMessage += "--Password needs an uppercase character.\n"

if pw.islower():
    errorMessage += "--Password needs a lowercase character.\n"

if pw.isalnum():
    errorMessage += "--Password needs a punctuation character.\n"

return errorMessage

passwordCandidate = input("Please input a valid password: ")
errors = ''

errors = checkPassword(passwordCandidate)

 if errors is not '':
    print("Your password is invalid:\n", errors, sep='')

elif user == "Alice" and passwordCandidate == "IamAlice123!" and 
passwordCandidate == "Alicerules99!" and passwordCandidate == 
"ILo^eBob2017" and passwordCandidate == "pa%%word2017":
    print("--It appears that you have used this password before. Please 
choose another one.")

elif user == "Bob" and passwordCandidate == "MyNameIs808,FearMe" and 
passwordCandidate == "hackerdude2017@@@" and passwordCandidate == 
"iamthe#1bestatLIFE" and passwordCandidate == "2Busy2BeTiedDown!":    
    print("--It appears that you have used this password before. Please 
choose another one.")

elif user == "Charles" and passwordCandidate == "Alice5uxAlice5ux!" and 
passwordCandidate == "GoAwayYuckyAlice" and passwordCandidate == 
"!secretlyLuvBob99" and passwordCandidate == "MarryMeB0b@~~~":
    print("--It appears that you have used this password before. Please 
choose another one.")

else:
    print("Your password is valid.")    

Photo of code

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    对于已使用的密码,您需要将and 运算符更改为or 运算符,并将其括在括号中。

    elif user == "Alice" and (passwordCandidate == "IamAlice123!" or passwordCandidate == "Alicerules99!" or passwordCandidate == "ILo^eBob2017" or passwordCandidate == "pa%%word2017"):
       print("--It appears that you have used this password before. Please choose another one.")
    

    【讨论】:

      猜你喜欢
      • 2015-08-05
      • 2017-11-29
      • 2013-02-06
      • 2010-09-28
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      相关资源
      最近更新 更多