简体   繁体   English

如何将循环添加到用户名和密码登录(Python)?

[英]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.

相关问题 如何循环输入密码/用户名登录页面并从外部文件读取密码/用户名? - How do I loop my password/username login page and read the password/username from an external file? 如何在Python的数据库列表中添加新的用户名和密码? - How do I add a new username and password to a database list in Python? 如何使用Selenium登录需要用户名和密码的站点? - How do I use Selenium to login to sites that require username and password? 如何修复此循环以进行简单的用户名和密码验证? - How do I fix this this loop for a simple username and password verification? 我如何在饰品 python 中设置密码和用户名? - How do i make a password and username in trinket python? 如何将最大登录尝试添加到用户名和密码来自数据库的 python 脚本? - How to add a maximum login attempt to a python script where the username and password is from the database? 如何使用python为xml格式添加用户名和密码 - How to add username and password for xml format with python 如何在Python中循环登录? - How do I loop this login in Python? 如何使用python代码使用用户名和密码登录数据库? - how to login into the database with username, password using python code? 如何使用 PySpark 和 SparkSession 设置到 HIVE 的连接(如何添加用户名和密码)? - How to setup connection to HIVE using PySpark and SparkSession (How do I add username and password)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM