0% found this document useful (0 votes)
4 views

Guessing game

The document presents a Python script for a guessing game where the player must guess a randomly generated number between 1 and 100. The game provides feedback on whether the guess is too low or too high and counts the number of attempts. The player wins when they correctly guess the number, and the game displays the number of attempts taken.

Uploaded by

jcoppola857
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Guessing game

The document presents a Python script for a guessing game where the player must guess a randomly generated number between 1 and 100. The game provides feedback on whether the guess is too low or too high and counts the number of attempts. The player wins when they correctly guess the number, and the game displays the number of attempts taken.

Uploaded by

jcoppola857
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Python 3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.

1937 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>>
>>> def guessing_game():
... print("Welcome to the Guessing Game!")
... number_to_guess = random.randint(1, 100)
... number_of_attempts = 0
... player_guess = None
...
... while player_guess != number_to_guess:
... player_guess = int(input("Guess a number between 1 and 100: "))
... number_of_attempts += 1
...
... if player_guess < number_to_guess:
... print("Too low! Try again.")
... elif player_guess > number_to_guess:
... print("Too high! Try again.")
...
... print(f"Congratulations! You guessed the correct number {number_to_guess}
in {number_of_attempts} attempts.")
...
>>> # Run the game
>>> guessing_game()
Welcome to the Guessing Game!
Guess a number between 1 and 100: 11
Too low! Try again.
Guess a number between 1 and 100: 33
Too low! Try again.
Guess a number between 1 and 100: 65
Too high! Try again.
Guess a number between 1 and 100: 50
Too low! Try again.
Guess a number between 1 and 100: 60
Too high! Try again.
Guess a number between 1 and 100: 59
Too high! Try again.
Guess a number between 1 and 100: 55
Too high! Try again.
Guess a number between 1 and 100: 54
Congratulations! You guessed the correct number 54 in 8 attempts.
>>>

You might also like