Here is this guessing game. I would like the player to try 2 times if he failed the game must stop.
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("Sorry, guess again. Too low.")
elif guess > random_number:
print("Sorry, Too high.")
print(f"Yay, congrats. You have guessed the number {random_number} correctly!!")
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.