Python code for " Guessing Game ". Computer science project
Here is a simple Python code for a guessing game:
import random
print("Welcome to the Guessing Game!")
print("I am thinking of a number between 1 and 100.")
number = random.randint(1, 100)
guess = None
count = 0
while guess != number:
try:
guess = int(input("Guess the number: "))
except ValueError:
print("Invalid input, please enter a number.")
continue
if guess < number:
print("Too low, try again!")
count += 1
elif guess > number:
print("Too high, try again!")
count += 1
else:
count += 1
print(f"Congratulations, you guessed the number in {count} tries!")
DESCIPTION:
In this code, the random module is used to generate a random number between 1 and 100. The user is prompted to guess the number and the program provides feedback if the guess is too low or too high. The game continues until the user correctly guesses the number. The program also keeps track of the number of guesses it took the user to guess the number.

Comments
Post a Comment