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

Lab Assignment 01

AI

Uploaded by

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

Lab Assignment 01

AI

Uploaded by

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

Lab Assignment 01: Artificial Intelligence

import random

# Start of the game


class Game:
def guessing_game(self):
lower_bound = 1
upper_bound = 100
# Generate a secret number
secret_number = random.randint(lower_bound, upper_bound)

# Instructions
Print (f"I'm thinking of a number between {lower_bound} and {upper_bound}.")

# Keep track of how many guesses the player makes


guesses_taken = 0

# Game loop
while True:
try:
# Ask the player to guess a number
guess = int(input("Enter your guess: "))
guesses_taken += 1 # Count each guess

# Check if the guess is correct


if guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
else:
print(f"Congratulations! You guessed the number in {guesses_taken} guesses.")
break # Exit the loop when the correct guess is made
except ValueError:
print("Please enter a valid number.")

# Create an instance of the game


g = Game()

# Start the game


g.guessing_game()

You might also like