简体   繁体   English

密码检查器循环不起作用

[英]Password Checker loop not working

I have tried to create an app in Python that makes a user create a password and then makes them verify it. 我试图用Python创建一个应用程序,该应用程序使用户创建密码,然后让他们验证密码。 Obviously I had to create a loop so that when the user entered an unmatching password they had to try again. 显然,我必须创建一个循环,以便当用户输入不匹配的密码时,他们必须再次尝试。 I am not very experienced with loops so here is what I got. 我对循环不是很有经验,所以这就是我得到的。 How do I make this work? 我该如何工作?

here is the code: 这是代码:

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)

here is another set of code trying to do the same thing: 这是另一组试图做同样的事情的代码:

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

Your conditional to test whether the passwords match was included in the loop which checks that they don't match -- so, it never gets run. 您的条件是测试密码是否匹配包含在检查密码不匹配的循环中-因此,它永远不会运行。

Try this instead: 尝试以下方法:

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')

Note how only the code that should be run if the passwords don't match is included within the while loop. 请注意,while循环中仅包含密码不匹配时应运行的代码。

What you need here is an "N and a half times loop". 您需要的是一个“ N次半循环”。 To avoid repeating code (guided by the DRY principle) you might consider writing a function to get the passwords. 为了避免重复代码(遵循DRY原则),您可以考虑编写一个函数来获取密码。 The following might work: 以下可能有效:

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()

The loop won't be entered at all if the original passwords match. 如果原始密码匹配,则根本不会输入循环。 Otherwise it will repeat until they do. 否则,它将重复直到他们这样做。

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...!"

I know this one is long, It is just for basic understanding 我知道这很长,只是为了基本了解

The other answers provide the code that will do what you want . 其他答案提供了将执行所需功能的代码 I thought that it could be interesting to tell you a little bit more what was wrong with the code you provided because the problem is more about your understanding of how things work. 我认为告诉您更多提供代码的问题可能很有趣,因为问题更多是关于您对事物工作方式的理解。

1st attempt: a variable and a function have the same name 第一次尝试:变量和函数具有相同的名称

# 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:
    [...]

This could have been avoided simply by changing the name of your function (which is never called by the way so I'm not sure what you wanted to do but I thought you might find that interesting). 只需更改函数名称即可避免这种情况(此方式从未被调用过,因此我不确定您要做什么,但我认为您可能会发现它很有趣)。 Further reading: Python: function and variable with same name 进一步阅读: Python:具有相同名称的函数和变量

2nd attempt: the values of the variables stay the same 第二次尝试:变量的值保持不变

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

The way to fix that is what the other answers provide: take your message out of the loop (getting out of the loop means that your condition was met) ; 其他答案提供的解决方法是:使您的信息脱离循环(脱离循环意味着您的条件得到满足); make sure that you do something inside the loop that will assure that the next test will be different from the previous one (otherwise, you will end up testing the same values over and over again) ; 确保在循环内做一些事情,以确保下一个测试将不同于上一个测试(否则,您将一遍又一遍地测试相同的值); to avoid breaking inside the loop, set your values outside the loop, and then get new values inside the loop in case the initial test failed. 为避免在循环内中断,请在循环外设置值,然后在循环内获取新值,以防初始测试失败。

Here's an idea: First define these: 这是一个想法:首先定义这些:

MinPass = 6
MaxPass = 12

Then this: 然后这样:

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

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM