用户输入验证将不会生效

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

我首先要感谢任何人和每个人,感谢您抽出宝贵的时间来帮助像我这样的磨砂膏,并感谢您为我提供帮助的时间。因此,我尝试制作一个简单的用户创建脚本。询问用户的名字和姓氏,将用户的名字的姓氏与姓氏连接起来,并用一个随机数连接起来以创建用户名。然后,我将提示用户创建密码,并使密码长度至少为6个字符。之后,我要求用户验证密码。我一直在发疯,因为当程序到达密码验证步骤时,它不会检查6个字符,也不验证密码是否相同,并继续执行程序的其余部分。

这是密码部分的代码段:

# 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")

并且在所有这些之后,我使用已生成的新信息进行“用户”登录。他们使用在第一部分中生成的用户名并使用在第二部分中输入的密码并进行检查。同样,它跳过检查并继续执行程序。我发疯了,因为我花了几个小时才学习一些基础知识,因为我是python的初学者。

这里是完整的代码:

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()
python input verification skip
1个回答
0
投票

因此,您的代码中有很多错误。第一个是,如果密码少于6个字符,没有什么可以阻止程序继续运行。其次,password == password将始终返回true,因为您正在对照自身检查var。我重新编写了一些代码,以尝试帮助您解决问题。我希望这有帮助!我还将代码拆分为几个函数+添加了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)
© www.soinside.com 2019 - 2024. All rights reserved.