Lucas
Lucas

Reputation: 11

How can I limit the number of user tries in this guessing game?

I have this code for a guessing game:

 import random

 

     def guess(x):
         random_number = random.randint(1,x)
         guess = 0
         while guess != random_number:
             guess = int(input(f"Guess a number between 1 and {x}: "))
             if guess < random_number:
                 print("Too low, try again! ")
             elif guess > random_number:
                 print("Too high, try again")
    
         print(f"You got the number {random_number} correctly" )
      guess(50)

But I get an error that says:

def guess(x):
    ^
IndentationError: expected an indented block after 'while' statement on line 537**

How can I fix the problem?

Upvotes: 0

Views: 853

Answers (1)

Random Davis
Random Davis

Reputation: 6857

Put something like num_guesses = 0 at the top of the function, then at the end of the while loop put in num_guesses += 1, then at the start of the loop put in something like if num_guesses < max_guesses: and have that print something and return. max_guesses can be a local or global variable, or you could make it another argument and pass it in.

Upvotes: 0

Related Questions