Quick actions

cmd+k|ctrl+k

Navigation

Languages

random game easier ver

Snippet info

Language

Python

Visibility

public

Author

miffyying621

Created

2025-04-07T09:57:15.152345Z

Updated

2025-04-07T10:20:43.183185Z

import random
def number_guessing_game():
    secret number = random.randint(1,100)
    attempts = 0
    max_attempts = 5
    
    print ("Welcome to Guessing Game")
    print (f"Think of a number between 1 to 100. You have {max_attempts}attempts.")
    while attempts < max_attempts:
        try: guess = int(input("Enter your guess: "))
        attempts +=1
        if guess == secret_number:
            print(f"Congrats! You guessed the number in {attempts}attempts!")
            return
        elif guess < secret number:
            print ("Oops too low. Please try again")
        else:
            print ("Oops too high. Please try again")
            
            print(f"attempts remaining: {max_attempts - attempts}")
            except ValueError:
                print ("Please enter a valid number")
                continue
            print(f"Game Over! The number was {secret number}")
INFO