我的账户创建程序在第一次验证后就不对输入进行验证了,有没有更有效的方法?

问题描述 投票:0回答:1

我的程序由一个菜单组成,提示用户是要注册、登录还是退出。现在,我正在处理注册的部分,它要求用户输入用户名,它应该确保用户名长度在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
1个回答
0
投票

问题是,这 or 这样,如果第一个条件返回 True 它不会检查其他的,只会继续执行代码。所以你需要改变的是,不要使用 or 你应该使用 and.

或者更简单的方法是让正则表达式像这个函数一样检查长度。

from re import match

def isUsernameValid(username):
    isValid = match(r"^[A-Za-z0-9_]{3,16}$", username)
    if isValid:
        return True
    else:
        return False

陌生人,祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.