【问题标题】:My account creation program doesn't validate the inputs after the first validation, is there a much more efficient way of doing it?我的帐户创建程序在第一次验证后不验证输入,有没有更有效的方法?
【发布时间】: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


    【解决方案1】:

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

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

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

    祝陌生人好运!

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2014-01-01
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      • 2020-09-29
      相关资源
      最近更新 更多