我正在对密码库进行基本的Python评估,但遇到了我似乎无法解决的错误。顺便说一句,我不是来这里参加语法和标点课程的,所以,如果您只是在这里编辑我的问题而不提供任何帮助,请不要打扰。
例如,在代码的这一部分中,我希望用户输入1或2,如果他选择1,它将要求他登录,而如果他选择2,它将要求他进行注册。但是目前,它完全忽略了参数并接受了任何东西。
另一个问题是,当用户输入有效密码时,不仅仅是出于正确的密码而停止输入密码,而是由于某种原因,它会再次询问“您的用户名是什么”。
while True:
login_orsignup1 = input('''Press
1) to Log in
2) to register a new account
''')
if login_orsignup1 != 1:
while True:
username = input('''What is your,
Username: ''')
if input_username == username:
l_p = input('''What is your password ''')
while True:
if l_p == input_lockerpassword:
print("Password Correct")
break
login_originup1()
----------------------------------------------------------#Full code begins now
l_p = ""
print("------------------------------------------------------------------------")
print('''Welcome to password Locker, a place where you can
store all your passwords to easily enter your precious accounts without
hassle.''')
print("------------------------------------------------------------------------")
print('''First lets make an account,''')
while True:
first_name = input('''What is your first name?
''')
if first_name.isdigit(): #isdigit, detects if there
print("Please enter a valid answer, No nubers shoud be present")
elif first_name == "":
print("Please enter an answer")
#the continue code skips the boundries within the loop and carries on with the connected program until it is succesfully met
else:
break #the break loop exits the current loop and continues with the next programes following it
while True:
sur_name = input('''What is your surname?
''')
if sur_name.isdigit(): #isdigit detects if the
print("No numbers")
elif sur_name == "":
print("Please enter an answer")
#the continue code skips the boundries within the loop and carries on with the connected program until it is succesfully met
else:
break
print('''------------------------------------------------------------------------''')
print('''Welcome, {} {}
what would you like your username to be, it should be something
memorable and no longer than fifteen characters long, '''.format(first_name, sur_name))
while True:
input_username = input("")
if 0 < len(input_username) < 16:
print('''Nice, username''')
break
elif input_username == "":
print("Please enter an answer")
else:
print('''Your username should be a maximum of 15 charecters, ''')
print('''-------------------------------------------------------------------------''')
while True:
input_lockerpassword = input('''Now it's time to setup a password for your locker, It should be between 4
and 10 charecters long,
''')
if len(input_lockerpassword) > 4 and len(input_lockerpassword) < 11:
print('''{}, is locked in thanks for joining Password Locker'''.format(input_lockerpassword))
break
else:
print("It should be between 4 and 10 charecters long!")
print('''
-------------------------------------------------------------------------------------------''')
def login_originup1():
print(''' Welcome to password vault, You can either login or create a New account''')
while True:
login_orsignup1 = input('''Press
1) to Log in
2) to register a new account
''')
if login_orsignup1 != 1:
while True:
username = input('''What is your,
Username: ''')
if input_username == username:
l_p = input('''What is your password ''')
while True:
if l_p == input_lockerpassword:
print("Password Correct")
break
login_originup1()```
答案 0 :(得分:0)
问题出在您的login_originup1
函数中,您正在执行三个While循环,而程序无法从您的函数中逃脱,您正在询问if login_orsignup1 != 1
没有else语句,因此如果用户要登录,他将按“ 1”中的输入,则程序将显示
"1" =! 1
是
它将查找else语句,但找不到一个,因此它将返回到循环的开头并要求用户再次输入。这是第一次循环。
现在,如果用户输入“ 2”(这意味着用户要注册),它将使他能够登录,原因是:
"2" =! 1
是
,并将继续到下一个while循环,在这里您将要求输入用户名,而用户将提供用户名。现在是第二个循环了
我们现在转到最后一个循环,在该循环中,您要求输入密码,然后用户将提供密码。程序将1.将其说为假并再次要求输入密码,或者2.将接受密码并中断While循环。现在是第三回合
那为什么要问我用户名,因为break语句只中断了while循环,所以break语句只中断了第三个while循环,然后返回到Second Loop,Second Loop将带我们回到第三循环
那该如何解决呢?
像这样的简单
def login_originup1():
print('Welcome to password vault, You can either login or create a New account')
while True:
login_orsignu = input('Press\n1) to Log in\n2) to register a new account\n')
loopBreaker = 0
if loopBreaker:
break
if int(login_orsignu) != 1:
while True:
if loopBreaker:
break
username = input('What is your,\nUsername:')
if input_username == username:
l_p = input('What is your password ')
while True:
if loopBreaker:
break
if l_p == input_lockerpassword:
print("Password Correct")
loopBreaker = 1
break
答案 1 :(得分:0)
好吧,首先,您应该知道input() function returns a string以及第一个条件if login_orsignup1 != 1
始终为真,因为字符串对象'1'
不是等于int对象1
。至于为什么输入了正确的密码后又会再次询问用户,这是因为break
语句仅从当前循环中中断。因此,您只需中断此循环即可回到用户名验证循环的开始。我建议这样更干净的实现:
# login or sign-up loop
while True:
login_orsignup1 = input(" Press \n1) to Log in \n2) to register a new account")
# you can have the input result in a variable like so, if you want to use it later on
if login_orsignup1 == "1": # you said '1' was for login, right?
# or you can test this directly in your if statement
# if it is only needed for this condition
while input("What is your username: ") != username:
print("Incorrect username")
# same thing for password, but could be implemented another way if you
# don't want to loop until the user gets the correct password
while input("What is your password: ") != input_lockerpassword:
print("Incorrect password for this username")
# your code gets here only if the user entered the right credentials
# so you can now break of the login or sign-up loop
break
elif login_orsignup1 == "2":
# code for registration here
对于一件简单的事情,这可能已经足够了。我建议通过遵循状态机的概念来设计此控制台程序,并在每个步骤中添加更多代码来处理诸如后退一步或从头开始的情况。
希望这会有所帮助