简体   繁体   English

我怎样才能 go 回到程序已经执行的行?

[英]How can I go back to a line that the program has already executed?

I am making a basic sign up and login in system, right now I'm building the log in part, the program asks the user for an input of a username which then checks the database of usernames, if the username is found it would then check for the password(going to work on that) but for now I'm looking at what would happen if the username was not found, okay it would ask again in case there was a spelling error after that it would ask the user for an input to whether they would like to back to the main menu but the issue is the main menu has already been executed at the beginning of the program and if I just copied and pasted it would make the program much longer and would be a paradox.我正在系统中进行基本注册和登录,现在我正在部分构建日志,程序要求用户输入用户名,然后检查用户名数据库,如果找到用户名,然后检查密码(继续工作),但现在我正在研究如果找不到用户名会发生什么,好吧它会再次询问,以防万一之后出现拼写错误,它会询问用户输入他们是否想回到主菜单,但问题是主菜单已经在程序开始时执行,如果我只是复制和粘贴它会使程序更长,并且会是一个悖论。 If it's still unclear please let me now.如果仍然不清楚,请让我现在。

This is my program:这是我的程序:

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: ")

As you can see at the end I ask for an input which the user can determine whether they want to start all over again at the beginning where they could sign up instead or try another username but I have no clue how to do that.正如您在最后看到的那样,我要求输入一个输入,用户可以确定他们是否想从头开始重新开始,他们可以注册或尝试另一个用户名,但我不知道如何做到这一点。 Thank you.谢谢你。

Just write everything into a while loop.只需将所有内容写入一个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: ")

When you want to exit the loop just break it.当你想退出循环时,只需打破它。 eg例如

elif menu == 3:
    break

Alternatively you could create a boolean exit = False before the loop, change the exit condition of your loop to while(not exit) and set exit = True when you want to exit it.或者,您可以在循环之前创建 boolean exit = False ,将循环的退出条件更改为while(not exit)并在要退出时设置exit = True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM