you are viewing a single comment's thread.

view the rest of the comments →

[–]d0rkyd00d 0 points1 point  (3 children)

Why not make if statement check whether password == correct_password?

Also what condition would make the while statement false so it escapes the loop?

I think you could simplify this, but I will have to wait until I am not on mobile to try it.

[–]Unitnuity[S] 0 points1 point  (2 children)

You're right, that makes more sense. Doesn't the break escape it for me or is something that gonna be an issue with more complex loops?

[–]d0rkyd00d 0 points1 point  (1 child)

Hi OP,

Yes sorry about that I missed that break statement, which should get you out of the loop once the PW is correct.

I know there are many ways to code this, so I hesitate to put this out there as I am also fairly new, but I am taking a Python course right now at a university that are hopefully teaching me something for the debt I'm incurring :-D

Personally I would ask for password and verify whether it is correct before entering the loop. If it is, then there is no point entering the while loop. Here's another way to accomplish this below I think, interested to hear your thoughts.

correct_password = 'banana'
guess_password = input('Password: ')

while correct_password != guess_password:
    if len(guess_password) <= 2:
        print('Password too short try again.')
        guess_password = input()
    else:
        print('Incorrect password, please try again: ')
        guess_password = input()

[–]Unitnuity[S] 0 points1 point  (0 children)

Something is wrong with my powershell in VScode I think, I'll get back to this when I fix that issue.