-2

I have just started to explore python recently.

So, I read about while else in python which sounds awesome but I am failing to execute a simple code, what am I am missing?

env: python 3.8.5 (anaconda)

guess = 0
answer = 5
attempts = 0
while answer != guess:
    attempts += 1
    guess = int(input("Guess: "))
    if attempts >= 3:
        break
else:
    print("You Failed")

According to my understanding, it should print "You Failed" after 3 inputs which is not 5. But upon execution, it doesn't do that.

Any insight on this would be very helpful.

Thanks.

Genocide_Hoax
  • 843
  • 2
  • 18
  • 42
  • 4
    From the top answer [here](https://stackoverflow.com/questions/3295938/else-clause-on-python-while-statement): "The else clause is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won't be executed." – Carcigenicate Mar 21 '21 at 22:28
  • 3
    Does this answer your question? [Else clause on Python while statement](https://stackoverflow.com/questions/3295938/else-clause-on-python-while-statement) – Brian61354270 Mar 21 '21 at 22:29
  • So you have its purpose backwards. Granted though, it is a difficult construct to read meaningfully. I honestly avoid it because it's kind of a corner use-case, and I really don't think it makes intuitive sense. I guess if you directly compare a loop to an `if` it makes sense, but a loop is not a simple conditional. – Carcigenicate Mar 21 '21 at 22:30
  • you are incrementing the value of `attempts` before you check for `answer == guess`. When it comes into the while loop, the attempts is already at 1. So you are giving yourself only 2 attempts. Thats another problem to fix in your code – Joe Ferndz Mar 21 '21 at 22:33
  • ok, so since I added a break statement there, the while condition became True and the else part didn't execute. Then in this particular scenario, I should have added the else part in the while if itself? like the old days? so it is not a good example for the concept. right? – Genocide_Hoax Mar 21 '21 at 22:38
  • @JoeFerndz attempts is initialized by 0. so, you will get 3 attempts, no issues there. – Genocide_Hoax Mar 21 '21 at 22:40

2 Answers2

0

It will be difficult to explain this in the comments section so I am going to try and answer it in the answer section:

Code initialization:

guess = 0
answer = 5
attempts = 0

Iteration 1: Then code goes into while loop for the first time:

while answer != guess:
    attempts += 1

attempts value is 1 as answer != guess i.e., 5 != 0 by initialization. However, user input = 0

Then you check if attempts is 3 or more. Here it checks 1 >= 3 which is False

if attempts >= 3:
    break

assume guess = 8

Iteration 2: while statement checked for the first time with user input value

while answer != guess:
    attempts += 1

Now attempts value is 2 as answer != guess i.e., 5 != 8 user input. However, user input = 1

Then you check if attempts is 3 or more. Here it checks 2 >= 3 which is False

if attempts >= 3:
    break

assume guess = 9

Iteration 3: while statement checked for the second time with user input value

while answer != guess:
    attempts += 1

Now attempts value is 3 as answer != guess i.e., 5 != 9 user input. However, user Input = 2

Then you check if attempts is 3 or more. Here it checks 3 >= 3 which is True

if attempts >= 3:
    break

You break out of the loop as attempts is 3 while user has input only twice. Also, the user input value (even if it is 5) does not matter anymore. You are exiting the loop before you check it again.

Since you broke out of the loop, it also does not go into the else clause. That's why you are unable to get the code to go to else clause. The way this program is designed, the code will go into else clause if you actually guessed the correct answer. The else clause gets triggered when the while condition fails. When you guess correctly, the while condition fails. Your print statement contradicts with what's actually happening.

Hope this helps you debug your code.

Here's how to fix the code:

answer = 5
attempts = 0
while guess := int(input("Guess: ")) !=answer:
    attempts += 1
    
    if attempts >= 3:
        print("You Failed")
        break
else:
    print ("Congrats on guessing the correct number")

Note: I am using the walrus operator. If you are not familiar with it, read more details about walrus operator here.

Tweaking your code, you can fix it like this:

answer = 5
attempts = 0
while attempts < 3:
    guess = int(input("Guess: "))
    if guess ==answer:
        print ("Congrats on guessing the correct number")
        break
    attempts +=1
else:
    print ('You Failed')
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
0

Your code looks fine, and why you didn't see the output you failed after 3 guesses is because of the break statement, it terminates automatically the while else look

JoeVenner
  • 30
  • 4
  • try to run the code and give 2 incorrect answers and the 3rd one as the correct answer. Or even better, try to give `5` as the first answer? See what you get an the answer. – Joe Ferndz Mar 21 '21 at 23:21