【发布时间】:2020-06-13 04:17:02
【问题描述】:
我首先要提前感谢任何人和每个人花时间帮助像我这样的磨砂膏,并感谢您花时间帮助我。所以我正在尝试制作一个简单的用户创建脚本。询问用户他们的名字和姓氏,将用户名字的第一个字母与他们的姓氏连接起来,并将其与一个随机数连接起来以创建他们的用户名。然后,我将提示用户创建密码,密码长度至少为 6 个字符。之后,我要求用户验证他们的密码。我一直在发疯,因为当程序到达密码验证步骤时,它不会检查 6 个字符或验证密码是否相同,而是继续程序的其余部分。
这是密码部分的sn-p:
# 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()
【问题讨论】:
-
password == password始终为True。您应该添加另一个变量来记住最后一个值(这是许多语言非常常见的逻辑)。
标签: python input verification skip