Reputation: 9
import time
age = input("How old? ")
guess = 0
count = 0
while guess != age:
guess = input("What's your guess? ")
if guess == age:
print("Correct!")
else:
print("Wrong!")
count = count + 1
print("You guessed wrong this many times:{1} ".format(count))
while count <= 0:
print("You messed up this many times: {0} ".format(count))
count = count - 1
time.sleep(5)
I'm new to python/coding, so forgive any mistakes or misunderstanding. I need to get rid of the line that says "you guessed wrong this many times:[4]" after I correctly guess the number, as shown here:
Instead, I need it to roll out the information like this:
Upvotes: 0
Views: 90
Reputation: 1432
You should place it on right place, in else
block
while guess != age:
guess = input("What's your guess? ")
if guess == age:
print("Correct!")
break
else:
print("Wrong!")
print("You guessed wrong this many times:{1} ".format(count+1))
count = count + 1
Upvotes: 1
Reputation: 11606
Use break
to exit the loop upon success
count = 1
while guess != age:
guess = input("What's your guess? ")
if guess == age:
print("Correct!")
break
print("Wrong!")
print(f"You guessed wrong this many times: {count}")
count += 1
Upvotes: 0
Reputation: 89
A break
statement would end the loop as soon as the user guesses correctly:
while guess != age:
guess = input("What's your guess? ")
if guess == age:
print("Correct!")
break #Ends 'nearest' loop
else:
print("Wrong!")
count = count + 1
print("You guessed wrong this many times:{1} ".format(count)) #Never gets here if correct
Read about break
https://www.tutorialspoint.com/python/python_break_statement.htm
Also, I'm assuming you want while count >= 0:
as while count <= 0:
will never run.
Upvotes: 0