[英]How do I add a loop to a username and password login (Python)?
I'm a beginner to programming.我是编程的初学者。 I am trying to make a program which will ask the user to create an account and then login with their details which they have just entered... Here is what I was starting with and now I'm kinda stuck because I also have a syntax error on line 16:
我正在尝试制作一个程序,该程序将要求用户创建一个帐户,然后使用他们刚刚输入的详细信息登录...这是我开始的内容,现在我有点卡住了,因为我也有语法第 16 行的错误:
from time import sleep
print ("Hey there! To enter the system you are required to sign in")
sleep(2.0)
while True:
choose=input("If you have an account please type 'SIGN IN', if you do not have an account please type 'CREATE'. ")
if choose=="SIGN IN":
print ("You have chosen to sign into your account")
break
elif choose=="CREATE":
print ("You have chosen to create an account")
break
username=input("Please enter a username: ")
password=input("Please enter a memorable password: ")
else:
print ("Please enter a valid option, 'SIGN IN' or 'CREATE'")
sleep(1.0)
So any help appreciated, I know im kinda dumb, ive only just started.所以任何帮助表示赞赏,我知道我有点愚蠢,我才刚刚开始。
I have fixed the syntax error on line 16 by removing the 'break' statement and indenting the following two lines.我通过删除“break”语句并缩进以下两行来修复第 16 行的语法错误。
from time import sleep
print ("Hey there! To enter the system you are required to sign in")
sleep(2.0)
while True:
choose=input("If you have an account please type 'SIGN IN', if you do not have an account please type 'CREATE'. ")
if choose =="SIGN IN":
print("You have chosen to sign into your account")
if choose=="CREATE":
print ("You have chosen to create an account")
username=input("Please enter a username: ")
password=input("Please enter a memorable password: ")
else:
print("Please enter a valid option, 'SIGN IN' or 'CREATE'")
sleep(1.0)
from time import sleep
print ("Hey there! To enter the system you are required to sign in")
sleep(2.0)
while True:
choose=input("If you have an account please type 'SIGN IN', if you do not have an account please type 'CREATE'. ")
if choose=="SIGN IN":
print ("You have chosen to sign into your account")
break
elif choose=="CREATE":
print ("You have chosen to create an account")
username=input("Please enter a username: ")
password=input("Please enter a memorable password: ")
break
else:
print ("Please enter a valid option, 'SIGN IN' or 'CREATE'")
sleep(1.0)
while True:
input_username = input("Please input a valid username:")
input_password = input("Please input a valid password:")
if input_username == username and input_password == password:
print("Successful login!")
break
else:
print("Either the username or the password is incorrect,please try again")
So this is what you wanted to have It has two while loops The first one is for choosing between the two options, the second one will process the login credentials if wrong, will repeat the login check for infinity.所以这就是你想要的它有两个while循环第一个用于在两个选项之间进行选择,第二个将处理登录凭据,如果错误,将重复登录检查无穷大。
PS: If there is no username or password variable it will throw an error PS:如果没有用户名或密码变量会抛出错误
OK so thanks to @minorwannabedev I've managed to work it out.好的,多亏了@minorwannabedev,我已经设法解决了。 Here is my new code:
这是我的新代码:
from time import sleep
login = False
username = False
password = False
print ("Hey there! To enter the system you are required to sign in")
sleep(2.0)
while True:
if login == True:
break
choose=input("If you have an account please type 'SIGN-IN', if you do not have an account please type 'CREATE':\n")
if choose=="CREATE":
print ("You have chosen to create an account")
username=input("Please create a username:\n")
password=input("Please enter a memorable password:\n")
elif choose =="SIGN-IN":
print("You have chosen to sign into your account")
while True:
input_username = input("Please input a valid username:")
input_password = input("Please input a valid password:")
if input_username == username and input_password == password:
print("Successful login")
login = True
break
else:
print("Either the username or the password is incorrect,please try again")
break
else:
print("Please enter a valid option, 'SIGN IN' or 'CREATE'")
sleep(1.0)
This took me a little while to work out but it now works!这花了我一点时间来解决,但现在可以了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.