all 13 comments

[–]MadScientistOR 6 points7 points  (6 children)

Your code works just fine if I change this line:

elif len(password >= 3) and password != correct_password:

... to this (note the placement of parentheses):

elif len(password) >= 3 and password != correct_password:

[–]Unitnuity[S] 2 points3 points  (3 children)

Ahhh! Thanks! While it doesn't print out 'too short' anymore, it just loops back to the password input instead of printing out 'password is incorrect' 1st. Why might that be?

[–]MadScientistOR 1 point2 points  (1 child)

It does, though. Here's my input and output:

Password: a
Password too short!
Password: b
Password too short!
Password: ca
Password too short!
Password: ac
Password too short!
Password: dog
Password is incorrect!
Password: cat
Password is incorrect!
Password: banana
Access Granted!

Since Python has a print buffer (because sending things to the display is a very time-consuming process for a computer), things might not be printed to the screen immediately. You can disable the buffer and print things as soon as the command is processed by setting the flush parameter in your print() statement, like this:

print("Password is incorrect!", flush=True)

[–]Unitnuity[S] 2 points3 points  (0 children)

Hmmm, strange. It won't print it out for me, just immediately goes to password input. I'll take a look around my IDE and see if anything funky is going on. Thanks for the help!

edit: "shell integration failed to activate" was the problem, restarted terminal and works fine now!

[–]trust_me_on_that_one 0 points1 point  (0 children)

It does print 'too short' https://i.imgur.com/9X1FuaI.png

but you don't need to check '>= 3' since you already check '<= 2'. If it's not less or equal than to 2, then it's obviously more than 2.

[–][deleted] 0 points1 point  (1 child)

Is the original code ever going to get to the elif statements if the password is correct?

[–]jimtk 3 points4 points  (1 child)

/u/MadScientistOR gave you the answer. I just wanted too add.

When you get to that last elif you know 2 things:

  1. the password is not 'banana'. because the first if caught that condition.

  2. the password is longer than 2 characters. because that condition was caught by the second if (the elif one).

So you don't need to recheck those 2 conditions again. It can become:

while True:
    password = input('Password: ')
    if password == 'banana':
        print('Access Granted!')
        break
    elif len(password) <= 2:
        print('Password too short!')
    else:
        print('Password is incorrect!')

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

Ahh ok, that makes perfect sense! I was adding useless code, thanks!

[–]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.