【发布时间】:2020-06-08 17:50:02
【问题描述】:
我正在系统中进行基本注册和登录,现在我正在构建部分登录,程序要求用户输入用户名,然后检查用户名数据库,如果找到用户名然后它会检查密码(继续工作)但现在我正在研究如果找不到用户名会发生什么,好吧它会再次询问以防万一之后出现拼写错误它会询问用户输入是否要返回主菜单,但问题是主菜单已经在程序开始时执行,如果我只是复制和粘贴它会使程序更长,并且会是悖论。如果还不清楚,请现在告诉我。
这是我的程序:
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: ")
正如您在最后看到的那样,我要求输入一个输入,用户可以确定他们是否要从头开始重新开始,他们可以注册或尝试其他用户名,但我不知道如何做到这一点.谢谢。
【问题讨论】:
-
这能回答你的问题吗? Restart function in python
标签: python python-3.x authentication menu