-1

Trying myself now at a little more simple stuff as a bloody python newbie:

Read into it here and there but still quite don't get it. This code works, but if entering a wrong password, it doesn't print out that the password is wrong. It just asks again to enter the right password, which is fine. But without printing that it's the wrong password. I like small, readable and simple code so I did it this way, but can you tell me where is my mistake?

Edit: I tried searching this in stackexchange and various google sites too. Although it's one of the most simple things, I always got examples that where way different and where I had a hard time understanding it. Now I got it. (First 2 answers)

while True:
    password = raw_input('Enter Password: ')
    if password == 'secret':
        print 'Password correct!'
        break
    if password == False:
        print 'Password incorrect!'

According to how I understood it: The code doesn't even get to run to the point of "if password == False:" and only does a "break" until the 'secret' has been entered. Could it be that I have to move the part

if password == False:
    print 'Password incorrect!'

somewhere else to it to work?

Thanks a lot for every kind help!

3 Answers3

2

I think you are looking for something like:

while True:
    password = raw_input('Enter Password: ')
    if password == 'secret':
        print 'Password correct!'
        break
    else:
        print 'Password incorrect!'

This will run true until the password is correct and then break out of the loop.

What you where trying to do is to check to if it was equal to 'secret' and then doing a separate check to see if the value is a boolean(false)

What the section above does is checks the password against 'secret' and if the user input is not equal to 'secret' this will return 'password incorrect'

Chris
  • 70
  • 5
  • Oh wow, it works! Thanks! I had the "else:" at the end too in my first tries, but there I had the upper part wrong. Many thanks for clearing up my mistakes! :) – BloodyPythonNewbie Jul 16 '17 at 20:22
  • No worries, anytime you have a problem come back and look through stack overflow it is a really great resource to have. – Chris Jul 16 '17 at 20:25
  • Yep it is. Sometimes only afraid of getting downvoted to hell because I am struggling of searching with the correct search terms. Always leads me to the wrong threads with different examples where I got a hard time understanding. Thanks again! – BloodyPythonNewbie Jul 16 '17 at 20:28
  • By the way: Code says "while True:" at the beginning. Is the "True" really needed for this? If so, why? Also I wouldn't know why there is no "False" needed then in the code. Thanks! – BloodyPythonNewbie Jul 16 '17 at 20:29
  • The while True just creates a continuous loop, it basically is doing a condition and a condition returns one of two values True and False, you are explicitly giving it True which means the condition will never change and cause the continuous loop and the way to come out of this is with the break – Chris Jul 16 '17 at 20:36
2

Just another way to do it.

while True:
    password = raw_input('Enter Password: ')
    if password == 'secret':
        print 'Password correct!'
        break
    print 'Password incorrect!'

When your password is correct the while loop will break. Otherwise the error message will be shown.

Kurohige
  • 1,378
  • 2
  • 16
  • 24
  • Tested it, works too. Thanks! But it seems for me reading and understanding the code with the "else:", which your version doesn't have, is a lot easier for me. – BloodyPythonNewbie Jul 16 '17 at 20:33
1

to test if password does not equal 'secret', add an else block.

while True:
    password = raw_input('Enter Password: ')
    if password=='secret':
        print 'Password Correct!'
        break
    else: #in other words, if password != secret
        print 'Password incorrect!'
sam-pyt
  • 1,027
  • 15
  • 26
  • Thanks a lot to you too! Works, just like Chris answered it too! :) Cleared up a lot for me! By the way before downvoting me: I did search a lot also on stackexchange but always seemed to get complicated and different threads. Thanks everyone! – BloodyPythonNewbie Jul 16 '17 at 20:25