[go: up one dir, main page]

0% found this document useful (0 votes)
4 views2 pages

Import Random

This document contains a Python script for a number guessing game where the player has to guess a randomly generated number between 1 and 100. The game provides feedback on whether the guess is too low or too high and tracks the number of attempts. If the player exceeds 10 attempts without guessing correctly, the game ends and reveals the target number.

Uploaded by

ojebson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Import Random

This document contains a Python script for a number guessing game where the player has to guess a randomly generated number between 1 and 100. The game provides feedback on whether the guess is too low or too high and tracks the number of attempts. If the player exceeds 10 attempts without guessing correctly, the game ends and reveals the target number.

Uploaded by

ojebson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import random

def play_guessing_game():

"""Plays a number guessing game."""

target_number = random.randint(1, 100)

attempts = 0

print("Welcome to the Number Guessing Game!")

print("I'm thinking of a number between 1 and 100.")

while True:

guess = input("Enter your guess: ")

try:

guess = int(guess)

except ValueError:

print("Invalid input. Please enter a number.")

continue

attempts += 1

if guess < target_number:

print("Too low!")

elif guess > target_number:

print("Too high!")

else:

print(f"You got it! The number is {target_number}!")


print(f"You guessed it in {attempts} attempts.")

break

if attempts > 10:

print(f"Out of attempts! The number was {target_number}.")

break

if __name__ == "__main__":

play_guessing_game()

You might also like