'How can I limit the number of user tries in this guessing game?
I'm completely new at this, I'm following a YouTube tutorial of a guessing game, however, after the game was finished, I wanted to limit the number of user tries to 3, using a while loop but I keep getting this error: I'm also aware I need to remove the while guess != random_number:
def guess(x):
^
IndentationError: expected an indented block after 'while' statement on line 537**
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)
How exactly do I do this?
Please and thank you!
Solution 1:[1]
Your code will run without errors if you remove the extra indentations of the function definition and everything inside/after it. The whole block under "import random" has an extra indentation, and this is what is causing the error. "def guess" should be at the same indentation level as the import statement, as should your function call at the end of the program.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Quack E. Duck |
