0

I've been experimenting with some stuff, and ran into this problem. The user must enter a password and has 3 attempts to do so, otherwise he will be shut out. But I keep getting 6 attempts. I am aware that I can fix this but using pw_count < pw_attempt instead of pw_count <= pw_attempt. I just want to understand the logic behind it when I use <=

a1 = ""
a2 = ""
a3 = ""
pw_count = 0
pw_attempt = 3
pw = input("Please enter your password: ")
pwre = input("Please re-enter your password: ")

while pw != pwre and pw_count <= pw_attempt:
    a1 = input("Your password doesn't match, please try again: ")
    pw_count += 1
    if a1 == pw:
        break
    else:
        a2 = input("Your password doesn't match, please try again: ")
    pw_count += 1
    if a2 == pw:
        break
    else:
        a3 = input("Your password doesn't match, please try again: ")
    pw_count += 1
    if a3 == pw:
        break

    if (pw == pwre and pw_count <= pw_attempt) or (a1 == pw and pw_count <= pw_attempt) or (a2 == pw and pw_count <= pw_attempt) or (a3 == pw and pw_count <= pw_attempt):
        print("Password is confirmed")
    else:
        print("You have entered the wrong password too many times")

I just expect the program to prompt the user with 3 attempts instead of 6.

Arsal
  • 447
  • 2
  • 6
mudi loodi
  • 103
  • 5
  • Wou get an IndentationError with this code. Please read [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Patrick Artner Aug 22 '19 at 12:33
  • 2
    your while is also obsolete - you have three inputs, no need to loop. Also: you enter the while a second time because `pw_count == pw_attempt` - and get another 3 times asked – Patrick Artner Aug 22 '19 at 12:35
  • Thanks guys, I did think of that to begin with, but I thought the loop would just terminate after the first attempt(a1) because the count would be at 4. But obviously it has to go through the entire loop before checking on the loop condition. – mudi loodi Aug 22 '19 at 12:46

4 Answers4

0

Since your test at while is pw_count <= pw_attempt, it will continue when pw_count is 3, ask 3 more times, which gets you to 6.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

You're asking for password 3 times in one iteration. You already know you can fix this with pw_count < pw_attempt. The reason for 6 times is because after one iteration (asking 3 times where pw_count=0). The condition pw_count <= pw_attempt will still be true as pw_count = pw_attempt now so it'll go on to ask for the password 3 more times, making a total of 6.

Arsal
  • 447
  • 2
  • 6
0

Really just follow the code and you will arrive at the answer. When I follow your code and just translate it from Python to English, I get this:

  • Ask for password and store in pw
  • Ask for re-enter (first attempt) and store in pwre
  • Does pwre differ from pw? (assume yes) and is count (0) smaller than or equal to pw_attempt (3)? (yes, smaller)
  • Ask the user 3 more times and increment count each time user fails (will end up with count = 3 after that)
  • Go back to loop head
  • Does pwre differ from pw? (assume yes) and is count (3) smaller than or equal to pw_attempt (3)? (yes, EQUAL!)
  • Ask the user 3 more times and increment count each time user fails (will end up with count = 6 after that.
  • Go back to loop head
  • Does pwre differ from pw? (assume yes) and is count (6) smaller than or equal to pw_attempt (3)? (no, greater, so loop terminates)
dvaergiller
  • 795
  • 3
  • 11
0

You can just simplify that like this:

pw_count = 0
pw_attempt = 3
pw = input("Please enter your password: ")
while pw_count < pw_attempt:
    pw_count += 1
    if pw_count == 1:
        pwre = input("Please re-enter your password: ")
    else:
        pwre = input("Your password doesn't match, please try again: ")
    if pw == pwre:
        print("Password is confirmed")
        break
Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23
  • Yes thank you, I am aware, I just wanted to experiment and see if I can write the code in different way but still make it do the same thing. – mudi loodi Aug 22 '19 at 12:53