Skip to content
Sign upLog in
← Back to Community

how do I get a user to have only 3 chances at guessing a number?

Profile icon
carynfisher

I am trying to implement a program that allows a user to guess a number. But they can only have 3 guesses and when they guess a correct number the program should stop.

Voters
Profile icon
SantiagoDE25
Profile icon
MargauxMdB
Profile icon
carynfisher
Comments
hotnewtop
Profile icon
carynfisher

Thank you so much!

Profile icon
AceHarvey

use a while loop:

guesses = 0 while guesses < 3: guess = int(input("Guess: ")) if guess == n: print ("Correct") break else: print("Incorrect") guesses+=1
Profile icon
Codejira

Create a counter variable increasing with every guess.
In a while loop you can check the number of guesses.
When the guess is correct leave the loop with the break statement.