我尝试在Python中创建一个应用程序,让用户创建一个密码,然后让他们验证它。显然我必须创建一个循环,以便当用户输入不匹配的密码时,他们必须再次尝试。我对循环不太熟悉,所以这就是我得到的。我如何使这项工作?
这是代码:
password = raw_input ("Create a password: ")
passwordv = raw_input ("Retype your password: ")
a = ("Passwords don't match! Please try again!: ")
b = ("Congrats, you have created a password")
def password():
if password == passwordv :
print ("Congrats, you have created a password")
else :
print (a)
return password
return passwordv
while password !=passwordv:
print (a)
这是另一组试图做同样事情的代码:
password = raw_input('Create a password: ')
passwordv = raw_input('Veryify your password: ')
while (password != passwordv):
print raw_input('Passwords do not match, try again: ')
if (password == passwordv) :
print ('Password set')
break
答案 0 :(得分:0)
您测试密码是否匹配的条件包含在循环中,该循环检查它们是否匹配 - 因此,它永远不会运行。
请改为尝试:
password = raw_input('Create a password: ')
passwordv = raw_input('Verify your password: ')
while (password != passwordv):
print raw_input('Passwords do not match, try again. ')
password = raw_input('Create a password: ')
passwordv = raw_input('Verify your password: ')
continue
print ('Password set')
请注意,如果密码不匹配,那么只应运行的代码包含在while循环中。
答案 1 :(得分:0)
这里你需要的是一个N次半循环"。为避免重复代码(由DRY原则引导),您可以考虑编写一个函数来获取密码。以下可能有效:
def get_passwords():
password = raw_input('Create a password: ')
passwordv = raw_input('Veryify your password: ')
return password, passwordv
pw1, pw2 = get_passwords()
while pw1 != pw2:
print "Sorry, passwords do not match"
pw1, pw2 = get_passwords()
如果原始密码匹配,则根本不会输入循环。否则它会重复,直到它们为止。
答案 2 :(得分:0)
password=raw_input('Enter password: \t\t\t')
passwordv=raw_input('Enter password again to verify: \t')
if password == passwordv:
print "\ncongratz you have created password"
else:
print "\nPlease try again...!"
while password != passwordv:
password=raw_input("Enter password: \t\t\t")
passwordv=raw_input("Enter password again to verify: \t")
if password == passwordv:
print"\ncongratz you have created password"
else:
print"\nPlease try again...!"
我知道这个很长,只是为了基本的理解
答案 3 :(得分:0)
其他答案提供将执行您想要的代码。我认为告诉你更多你提供的代码有什么问题可能会很有趣,因为问题更多的是你对事物运作方式的理解。
# Here you define your password variable as a string
password = raw_input ("Create a password: ")
[...]
# And then you reassign it here: it contains now the address of that function!
def password():
[...]
# Now you're comparing a function to a string: they'll never be equal
while password !=passwordv:
[...]
这可以通过更改你的功能名称来避免这种情况(这种情况从未被调用过,因此我不确定你想做什么,但我认为你可能会觉得这很有趣)。进一步阅读:Python: function and variable with same name
password = raw_input('Create a password: ')
passwordv = raw_input('Veryify your password: ')
# At this point, if the test is true, you'll reach the end of the code without
# displaying the 'Password set' message you wanted
while (password != passwordv):
# This only prints a message
print raw_input('Passwords do not match, try again: ')
# The condition below can never be true: you just tested that they were
# different but did nothing to give them new values!
if (password == passwordv) :
print ('Password set')
break
解决问题的方法是其他答案提供的方法:将你的信息从循环中取出(离开循环意味着你的条件得到满足);确保你在循环中做了一些事情,以确保下一个测试与前一个测试不同(否则,你将最终反复测试相同的值);为了避免在循环内部破坏,在循环外设置值,然后在初始测试失败的情况下在循环内获取新值。
答案 4 :(得分:0)
这是一个想法: 首先定义这些:
MinPass = 6
MaxPass = 12
然后这个:
print ("Now, lets try with a password. Please enter one here")
EnteredPassword = input("Password: ")
while len(EnteredPassword) < MinPass:
print ("That password is too small. Please try again")
EnteredPassword = input("Password: ")
while len(EnteredPassword) > MaxPass:
print ("That password is too long, please try again")
EnteredPassword = input("Password: ")
希望这有帮助!