对用户在 Python 中输入的数据验证



我目前正在进行我的自定进度项目,我在数据验证过程中遇到了问题。

def password_option():
choice = input("nWhich password do you prefer to use?nEnter your choice by number: ")
if choice == str("1"):
return "Your password is " + password_1
elif choice == str("2"):
return "Your password is " + password_2
else:
print("Your choice is invalid. Please choose it again.")
return password_option()

print(password_option())

def password_validation():
valid = input("nPlease re-enter your password to verify: ")
if valid == password_1 and password_option() == str("1") or valid == password_2 and password_option() == str("2"):
return" ***** Congratulations, you had entered Vandyck's server. ***** "
else:
print("This is not a valid password. Please try again.")
return password_validation

print(password_validation())

所以基本上用这段代码,我正在考虑验证我的用户的输入并显示不同的输出,从而确定他们正在插入他们在第一个def()函数中选择的正确输入。

这段代码的输出非常令人困惑,在我在运行终端的第二个def()函数中插入一些东西后,它继续给我在第一个def()函数中设置的输出行。插入我选择的密码后,我得到的输出返回到"哪个密码你更喜欢使用…"。这不是我所期望的,因为在我插入密码后,它应该显示我"* * * * *祝贺. .";或者如果密码错误,应该显示"这是……并返回到有效的输入行。

我可以知道如何解决这个问题吗?

据我所知,我想这会对你有所帮助:

def password_option():
choice = input("nWhich password do you prefer to use?nEnter your choice by number: ")
if choice == str("1"):
print("You have chosen option number 1")
return choice
elif choice == str("2"):
print("You have chosen option number 2")
return choice
else:
print("Your choice is invalid. Please choose it again.")
return password_option()

choice = password_option()

def password_validation(choice=choice):
valid = input("nPlease re-enter your password to verify: ")
if valid == password_1 and choice == str("1") or valid == password_2 and choice == str("2"):
return" ***** Congratulations, you had entered Vandyck's server. ***** "
else:
print("This is not a valid password. Please try again.")
return password_validation()

print(password_validation())

您在第二个函数中再次调用了第一个函数。您必须将用户输入作为变量返回到代码中,以便它记住用户的选择,然后在第二个函数中使用该变量进行验证。

相关内容

  • 没有找到相关文章

最新更新