Python用户注册:无法阻止用户在 Python上注册重复的用户名

我是一个正在做我的 Python 作业的学生。问题是,我们的讲师对我们不使用 python 内置的非常严格,并且在提交之前拒绝检查我们的代码,并且我在注册过程中最难让我的代码正常工作。

我目前的问题是,当用户尝试使用已获取的用户名进行注册时,我希望代码提示程序本身出现错误。但是,无论我如何尝试修复它,它都会不断运行不同的错误。任何人都可以请指出我正确的方向或告诉我我的代码有什么问题吗?

def grant():
    print("Helllooooo")
def begin():
    while True:
        print("Welcome to the Freshco Groceries App!")
        option = input("Are you a new or returning user?\n"
                       "Enter 1 if you would like to LOGIN\n"
                       "Enter 2 if you would like to register\n"
                       "Enter 3 if you would like to exit the program\n")
        if option=="1":
            access(option)
            break
        elif option=="2":
            access(option)
            break
        elif option=="3":
            print("Thank you! Have a good day.")
            exit()
        else:
            continue
        
def access(option):
    if(option=="1"):
        name = input("Please enter your username:\n")
        password = input("Please enter your password:\n")
        login(name,password)
    else:
        print("First step: Please choose a username and a unique password.")
        name = input("Please enter your username:\n")
        password = input("Please enter your password:\n")
        register(name,password)
        newuser()
def login(name,password):
    success=False
    file = open("user_details.txt","r")
    for line in file:
        a,b = line.split(",")
        if (name in a) and (password in b):
            success=True
            break
    file.close()
    if(success):
        print("You have successfully logged in! Happy shopping!")
        grant()
    else:
        print("Wrong username or password entered.")
def register(name,password):
    exist = False
    while True:
        file = open("user_details.txt", "r")
        for line in file:
            line = line.rstrip()
            if (name == line):
                exist = True
                break
            else:
                pass
        file.close()
        if (exist):
            print("Username has been taken. Please try again with a new one.")
            break
        else:
            file = open("user_details.txt","a")
            file.write("\n" + name + "," + password)
            print("You have successfully registered! Welcome to the Freshco Family!")
            grant()
            break
def newuser():
    print("Before you start using the app, please fill in the details below.")
    alldata = []
    while True:
        newdata = []
        nname = input("Please enter your full name:\n")
        newdata.append(nname)
        while True:
            ngender = input("Are you female or male?\n"
                            "1: female\n"
                            "2: male\n")
            if ngender=="1":
                    fingender="female"
                    newdata.append(fingender)
                    break
            elif ngender=="2":
                    fingender="male"
                    newdata.append(fingender)
                    break
            else:
                print("INVALID INPUT")
                continue
        naddress = input("Please enter your address:\n")
        newdata.append(naddress)
        nemail = input("Please enter your email address:\n")
        newdata.append(nemail)
        ncontact = input("Please enter your contact number:\n")
        newdata.append(ncontact)
        ndob = input("Please enter your dob in this format: <DD.MM.YY>\n")
        newdata.append(ndob)
        alldata.append(newdata)
        print(newdata)
        break
    print("Thank you for entering your information, you will be redirected now!")
    grant()
begin()

太感谢你了!T-T

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(172)
C裤时装表演:Python超时装饰器
上一篇
Pop l:从列表中弹出随机元素的最Python的方法是什么
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(88条)