0
user_answer = (raw_input('Can you guess the word that is missing? ')).lower()
count =0
while count <=1:
     if user_answer == 'brown':
          print ('Well done')
          break
     else:
          user_answer =(raw_input('Please Guess again? '))
          count+=1
else:
     print ("Fail Game")

I'm working on a simple game and it allows the user to input the wrong guess three times. I have been playing around with this while loop and it works, (while count <=1) but i am a little confused as too why??? (not complaining) but can anyone explain why it works as i originally though the code should be like to one below (but these uses 5 attempts)

count = 0
while count <=3:

all rest of code is same as above.

1 Answer 1

2

The maximum number of inputs you can get with that code is 3. Why?

  • The first is outside the loop.
  • The second is when count is 0. After the input, count will be 1.
  • The third (and last) is when count is 1. After the input, count will be 2. The loop will finish in the next iteration because the condition 2 <= 1 will be False.
Sign up to request clarification or add additional context in comments.

1 Comment

i didn't think about the 1st instance of the first one outside the loop. Makes sense now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.