首页 > 解决方案 > 我怎样才能回到程序已经执行的行?

问题描述

我正在系统中进行基本注册和登录,现在我正在部分构建日志,程序要求用户输入用户名,然后检查用户名数据库,如果找到用户名,然后检查密码(继续工作),但现在我正在研究如果找不到用户名会发生什么,好吧它会再次询问,以防万一之后出现拼写错误,它会询问用户输入他们是否想回到主菜单,但问题是主菜单已经在程序开始时执行,如果我只是复制和粘贴它会使程序更长,并且会是一个悖论。如果仍然不清楚,请让我现在。

这是我的程序:

import sys, re, csv
from re import match
def isUsernameValid(username):
    isValid = match(r"^[A-Za-z0-9_]{3,16}$", username)
    if isValid:
        print("True")
        return True
    else:
        print("False")
        return False
def isUsernameFree(username):
        with open('accountdatabase.txt', mode = 'r') as file:
                reader = csv.reader(file, delimiter=',')
                for line in file:
                        if username == line.split(',')[0]:
                                print("False")
                                return False

                        else:
                                print("True")
                                return True


usernamecheck = False
charcheck = False
menu = int(input("1. Sign Up\n2. Log in\n3. Exit\nInput: "))
menu_numbers = (1,2,3)
while menu not in menu_numbers:
    menu = int(input("1. Sign Up\n2. Log in\n3. Exit\nInput: "))
if menu == 1:
        newusername = input("Input a new username: ")
        usernamecheck = isUsernameValid(newusername) and isUsernameFree(newusername)
        while usernamecheck == False:
                newusername = input("Input a new username: ")
                usernamecheck = isUsernameValid(newusername) and isUsernameFree(newusername)
        newpassword = input("Input a password: ")
        while len(newpassword)<8:
               newpassword = input("Input a password that has 8 or more characters: ")
        validatepassword = input("Input the same password: ")
        while newpassword != validatepassword:
                newpassword = input("Input a password: ")
                while len(newpassword)<8:
                        newpassword = input("Input a password that has 8 or more characters: ")
                validatepassword = input("Input the same password: ")
        with open('accountdatabase.txt', mode = 'a') as file:
                file.write(str(newusername) + "," + str(newpassword))

elif menu == 2:
    usernamesearch = input("Username: ")
    with open('accountdatabase.txt', mode = 'r') as file:
        reader = csv.reader(file, delimiter=',')
        for line in file:
            if usernamesearch == line.split(',')[0]:
                print(f'Username ({usernamesearch}) found.')
                accountfound = True
            else:
                print(f'Username not found.')
                accountfound = False

    while accountfound == False:
        usernamesearch = input("Username: ")
        with open('accountdatabase.txt', mode = 'r') as file:
            reader = csv.reader(file, delimiter=',')
            for line in file:
                if usernamesearch == line.split(',')[0]:
                    print(f'Username ({usernamesearch}) found.')
                    accountfound = True
                else:
                    print(f'Username not found.')
                    accountfound = False
                    menu = input("Choose 1 to go back to menu and 2 to keep trying: ")

正如您在最后看到的那样,我要求输入一个输入,用户可以确定他们是否想从头开始重新开始,他们可以注册或尝试另一个用户名,但我不知道如何做到这一点。谢谢你。

标签: pythonpython-3.xauthenticationmenu

解决方案


只需将所有内容写入一个while循环即可。

while (True):
    usernamecheck = False
    charcheck = False
    menu = int(input("1. Sign Up\n2. Log in\n3. Exit\nInput: "))
    menu_numbers = (1,2,3)
    while menu not in menu_numbers:
        menu = int(input("1. Sign Up\n2. Log in\n3. Exit\nInput: "))
    if menu == 1:
            newusername = input("Input a new username: ")
            usernamecheck = isUsernameValid(newusername) and isUsernameFree(newusername)
            while usernamecheck == False:
                    newusername = input("Input a new username: ")
                    usernamecheck = isUsernameValid(newusername) and isUsernameFree(newusername)
            newpassword = input("Input a password: ")
            while len(newpassword)<8:
                newpassword = input("Input a password that has 8 or more characters: ")
            validatepassword = input("Input the same password: ")
            while newpassword != validatepassword:
                    newpassword = input("Input a password: ")
                    while len(newpassword)<8:
                            newpassword = input("Input a password that has 8 or more characters: ")
                    validatepassword = input("Input the same password: ")
            with open('accountdatabase.txt', mode = 'a') as file:
                    file.write(str(newusername) + "," + str(newpassword))

    elif menu == 2:
        usernamesearch = input("Username: ")
        with open('accountdatabase.txt', mode = 'r') as file:
            reader = csv.reader(file, delimiter=',')
            for line in file:
                if usernamesearch == line.split(',')[0]:
                    print(f'Username ({usernamesearch}) found.')
                    accountfound = True
                else:
                    print(f'Username not found.')
                    accountfound = False

        while accountfound == False:
            usernamesearch = input("Username: ")
            with open('accountdatabase.txt', mode = 'r') as file:
                reader = csv.reader(file, delimiter=',')
                for line in file:
                    if usernamesearch == line.split(',')[0]:
                        print(f'Username ({usernamesearch}) found.')
                        accountfound = True
                    else:
                        print(f'Username not found.')
                        accountfound = False
                        menu = input("Choose 1 to go back to menu and 2 to keep trying: ")

当你想退出循环时,只需打破它。例如

elif menu == 3:
    break

或者,您可以在循环之前创建一个布尔值exit = False,将循环的退出条件更改为while(not exit)并设置exit = True您想要退出的时间。


推荐阅读