我正在尝试创建一个基本程序,要求用户输入然后重新输入密码。如果匹配,则会打印“密码已创建”。如果没有,它将继续要求用户重试,直到密码匹配。
这是我到目前为止所拥有的;我知道一个循环,至少需要一个“if”语句,但我不知道该怎么做。
password=raw_input("please select a password")
password_again=raw_input("please re-type your password")
loop=raw_input("Password does not match. Please try again")
if password_again==password:
print("password created")
else: raw_input(loop)
非常感谢帮助!
答案 0 :(得分:1)
我认为使用getpass模块是一个明智的决定:它会提示用户输入密码而不回显
from getpass import getpass
print "Please select a password: "
passwd = getpass()
print "Please re-type your password: "
if passwd == getpass():
print "password created"
else:
print "Password does not match. Please try again"
输出:
Please select a password:
Password:
Please re-type your password:
Password:
Password does not match. Please try again
答案 1 :(得分:0)
这是了解while循环的好方法
询问价值
password=raw_input("please select a password")
password_again=raw_input("please re-type your password")
循环直到匹配
while password_again != password:
print "They don't match please try again"
password=raw_input("please select a password ")
password_again=raw_input("please re-type your password ")
答案 2 :(得分:0)
这不是循环,请使用while True
和break
:
while True:
password=raw_input("please select a password")
password_again=raw_input("please re-type your password")
if password_again==password:
print("password created")
break
else:
print "Password does not match. Please try again"
答案 3 :(得分:0)
最简单的方法是使用像这样的iter
函数
password = raw_input("please select a password")
for re_password in iter(raw_input, password):
print "Password does not match. Please try again :",
print "Password accepted!"