简体   繁体   English

用户输入验证不会坚持

[英]User input validation will not stick

I first want to thank anyone and everyone in advance for taking the time to help a scrub like me and appreciate your time in giving me a helping hand.我首先要提前感谢任何人和每个人花时间帮助像我这样的磨砂膏,并感谢您给予我帮助的时间。 So I am attempting to make a simple user creation script.所以我试图制作一个简单的用户创建脚本。 Asking the user for their first and last name, concatenated the user's first letter of their first name with their last and concatenating it with a random number to create their user name.询问用户他们的名字和姓氏,将用户名字的第一个字母与他们的姓氏连接起来,并将其与一个随机数连接起来以创建他们的用户名。 I then will prompt the user to create a password and have the password be a minimum of 6 characters long.然后我会提示用户创建一个密码,并且密码长度至少为 6 个字符。 After that, I ask the user to verify their password.之后,我要求用户验证他们的密码。 I've been going crazy because when the program reaches the password verification step, it doesn't check for the 6 characters or verify that the passwords are the same and continues to the rest of the program.我已经疯了,因为当程序到达密码验证步骤时,它不会检查 6 个字符或验证密码是否相同,而是继续执行程序的其余部分。

This is a snippet of the password part:这是密码部分的片段:

# Ask the user for a password that's at least 6 characters long

 while True:
    password = input("Enter a password for this account: ")
# Verify that the user's input is 6 characters long

    if len(password) < 6:
        print("Your password must be at least 6 characters long! ")
# Has the user verify the password

    password = input("Please verify your password by typing it in again: ")
    if password == password:
        print("Thank you for confirming your password")
    else:
        print("Nope your password did not match")

And after all of that, I am having the "user" login with the new information that was generated.毕竟,我让“用户”使用生成的新信息登录。 Using the username generated in the first part and using the password they input in the second and checking.使用第一部分中生成的用户名并使用他们在第二部分中输入的密码并进行检查。 The same thing, it skips the check and continues with the program.同样的事情,它跳过检查并继续执行程序。 I am going insane because I've spent a couple of hours just learning some basics as I am a beginner with python.我快疯了,因为我花了几个小时才学习一些基础知识,因为我是 Python 的初学者。

Here is the full code:这是完整的代码:

def main():
print("You do the typin, I will take care of the rest!")

#User will be prompted to input their first and last name
firstname = input("Please give me your first name. ")
lastname = input("Thank you, now please give me your last name. ")

# The first and last name will be concatenated and the first letter of the
# users name will be attatched to their last name.

username = firstname[0] + lastname[:7]

# Now to generate the random number from 100-999 to attach to the new
# username

import random
from random import randint

print("Welcome", username + str(random.randint(100,999)))

import re

def sub():

# Ask the user for a password that's at least 6 charcaters long
 while True:
    password = input("Enter a password for this account: ")
# Verify that the users input is 6 charcters long
    if len(password) < 6:
        print("Your password must be at least 6 charcaters long! ")
# Has the user verify the password
    password = input("Please verify your password by typing it in again: ")
    if password == password:
        print("Thank you for confirming your password")
    else:
        print("Nope your password did not match")
# Now the user must login using the generated username from above
    username = input("Enter your generated username! ")
    if username == username:
        print("Correct!")
    else:
        print("I have never seen you before!")
    password = input("Now enter your accounts password: ")
    if password == password:
        print("You are now logged in!")
    else:
        print("FAIL")
    break
main()
sub()

So, there are many errors in your code.因此,您的代码中有很多错误。 The first one is, there's nothing that stops the program from progressing if the password is less than 6 characters.第一个是,如果密码少于 6 个字符,则没有什么可以阻止程序继续进行。 Second, password == password will ALWAYS return true, because you're checking a var against itself.其次, password == password将始终返回 true,因为您正在检查自身的 var。 I re-wrote a bit of your code to try to help clarify your problem.我重新编写了一些您的代码以尝试帮助澄清您的问题。 I hope this helps!我希望这有帮助! I also split the code into a few functions + added getpass ( https://docs.python.org/3/library/getpass.html )我还将代码拆分为几个函数 + 添加了 getpass ( https://docs.python.org/3/library/getpass.html )

from getpass import getpass # You can use this module to hide the password the user inputs
from random import randint

def generate_username():
    # Basic username generation, same thing you did
    print("You do the typin, I will take care of the rest!")
    firstname = input("Please give me your first name. ")
    lastname = input("Thank you, now please give me your last name. ")
    username = firstname[0] + lastname[:7] + str(randint(1, 99))
    # You can't concatenate strings and ints, so I convert the number to a string first
    print(f"Your username is: {username}") # f-strings (https://realpython.com/python-f-strings/)
    return username



def generate_password():
    while True:
        password = getpass("Enter a password for this account: ")
        confirm_password = getpass("Enter your password again: ") # Ask the user to enter the password a second time to confirm
        if password != confirm_password: # Check if the user entered the same password
            print("Passwords dont match!")
        elif len(password) < 6: # Check the length
            print("Your password must be at least 6 charcaters long! ")
        else: # If everythings all good
            print("Password is valid!")
            return password # Break the loop and return password value

def login(username, password):
    # Used to login a user
    while True:
        entered_username = input("Enter your username: ")
        entered_password = getpass("Enter your password: ")
        if username == entered_username and password == entered_password:
            # Confirm if username and password are correct, then exit the loop (or do something else)
            print("Login successful!")
            break
        else:
            print("Login failed, please confirm your username and password")

username = generate_username()
password = generate_password()
login(username, password)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM