[text] guessing game

Viewer

copydownloadembedprintName: guessing game
  1. import random
  2.  
  3. def guessing_game():
  4.     print("Welcome to the Guessing Game!")
  5.     print("I'm thinking of a number between 1 and 100.")
  6.     print("Try to guess the number.")
  7.  
  8.     secret_number = random.randint(1, 100)
  9.     attempts = 0
  10.  
  11.     while True:
  12.         guess = int(input("Enter your guess: "))
  13.         attempts += 1
  14.  
  15.         if guess < secret_number:
  16.             print("Too low! Try again.")
  17.         elif guess > secret_number:
  18.             print("Too high! Try again.")
  19.         else:
  20.             print(f"Congratulations! You guessed the number in {attempts} attempts.")
  21.             break
  22.  
  23. guessing_game()
  24.  
  25.  

Editor

You can edit this paste and save as new:


File Description
  • guessing game
  • Paste Code
  • 09 Jun-2023
  • 637 Bytes
You can Share it: