簡體   English   中英

如何將循環添加到用戶名和密碼登錄(Python)?

[英]How do I add a loop to a username and password login (Python)?

我是編程的初學者。 我正在嘗試制作一個程序,該程序將要求用戶創建一個帳戶,然后使用他們剛剛輸入的詳細信息登錄...這是我開始的內容,現在我有點卡住了,因為我也有語法第 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)

所以任何幫助表示贊賞,我知道我有點愚蠢,我才剛剛開始。

我通過刪除“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")

所以這就是你想要的它有兩個while循環第一個用於在兩個選項之間進行選擇,第二個將處理登錄憑據,如果錯誤,將重復登錄檢查無窮大。

PS:如果沒有用戶名或密碼變量會拋出錯誤

好的,多虧了@minorwannabedev,我已經設法解決了。 這是我的新代碼:

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)

這花了我一點時間來解決,但現在可以了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM