【发布时间】:2020-06-05 11:09:29
【问题描述】:
我的程序包含一个提示用户的菜单,询问他们是否要注册、登录或退出。现在,我正在注册部分,它要求用户输入用户名,它应该确保用户名长度在 3-16 个字符之间,尚未使用并且仅包含以下字符:A -Za-z0-9_ 这里的问题是它验证一次并且有某种错误忽略验证用户名的长度是否小于 3 或大于 16 或具有特殊字符,但可以检查是否用户名是否已使用。输入用户名后,它会要求用户输入密码,这似乎工作正常。在所有输入之后,它将用户名和密码添加到存储在计算机上的 txt 数据库中。如果有更有效的方法,请告诉我,我们将不胜感激。
这是程序:
import sys, re, csv
usernamecheck = False
charcheck = False
menu = int(input("1. Sign Up\n2. Log in\n3. Exit"))
menu_numbers = (1,2,3)
while menu not in menu_numbers:
menu = int(input("1. Sign Up\n2. Log in\n3. Exit"))
if menu == 1:
newusername = input("Input a new username: ")
if (not re.match("^[A-Za-z0-9_]*$", newusername)) or len(newusername)<3 or len(newusername)>16:
firstusernamevalidation = False
print("Username length is either less than 3 or more than 16 or has special characters.")
else:
firstusernamevalidation = True
with open('accountdatabase.txt', mode = 'r') as file:
reader = csv.reader(file, delimiter=',')
for line in file:
if newusername == line.split(',')[0]:
secondusernamevalidation = False
print("Username is taken, try another one.")
else:
secondusernamevalidation = True
while firstusernamevalidation and secondusernamevalidation == False:
newusername = input("Input another username: ")
if (not re.match("^[A-Za-z0-9_]*$", newusername)) or len(newusername)<3 or len(newusername)>16:
firstusernamevalidation = False
print("Username length is either less than 3 or more than 16 or has special characters.")
else:
firstusernamevalidation = True
with open('accountdatabase.txt', mode = 'r') as file:
reader = csv.reader(file, delimiter=',')
for line in file:
if newusername == line.split(',')[0]:
secondusernamevalidation = False
print("Username is taken, try another one.")
else:
secondusernamevalidation = True
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))
【问题讨论】:
标签: python python-3.x validation account user-accounts