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

Ailab 10-11

The document describes two Python programs: one for a search bot that retrieves information based on user input using the 'googlesearch-python' module, and another for a 'Guess the Number' game where users attempt to guess a randomly generated number within a specified range. The search bot handles exceptions and provides search results, while the game provides feedback on guesses and limits attempts. Both programs are designed to run in a loop until the user decides to exit.

Uploaded by

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

Ailab 10-11

The document describes two Python programs: one for a search bot that retrieves information based on user input using the 'googlesearch-python' module, and another for a 'Guess the Number' game where users attempt to guess a randomly generated number within a specified range. The search bot handles exceptions and provides search results, while the game provides feedback on guesses and limits attempts. Both programs are designed to run in a loop until the user decides to exit.

Uploaded by

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

10.

Build a bot which provides all the information related to text in search box

do this if you get an error like "no module named googlesearch"


pip install googlesearch-python

from googlesearch import search


def search_info(query):
try:
search_results = list(search(query, num_results=5, lang="en"))
return search_results
except Exception as e:
print(f"An error occurred: {e}")
return []

if __name__ == "__main__":
while True:
search_text = input("Enter text to search (or type 'exit' to quit): ")
if search_text.lower() == "exit":
break

results = search_info(search_text)
if results:
print(f"Search results for '{search_text}':")
for i, result in enumerate(results):
print(f"{i+1}. {result}")
else:
print(f"No results found for '{search_text}'.")

11. Implement any Game and demonstrate the Game playing strategies

import random
class GuessTheNumberGame:
def __init__(self, max_attempts=5, min_num=1, max_num=100):
self.min_num = min_num
self.max_num = max_num
self.secret_number = random.randint(min_num, max_num)
self.max_attempts = max_attempts
self.attempts = 0
def play(self):
print("Welcome to Guess the Number Game!")
print(f"I'm thinking of a number between {self.min_num} and
{self.max_num}.")
while self.attempts < self.max_attempts:
guess = self.get_guess()
if guess == self.secret_number:
print(f"Congratulations! You guessed the number
{self.secret_number} correctly!")
break
elif guess < self.secret_number:
print("Too low! Try again.")
else:
print("Too high! Try again.")
self.attempts += 1
else:
print(f"Sorry, you've run out of attempts! The correct number was
{self.secret_number}.")
def get_guess(self):
while True:
try:
guess = int(input(f"Guess the number ({self.min_num} -
{self.max_num}): "))
if self.min_num <= guess <= self.max_num:
return guess
else:
print(f"Please enter a number between {self.min_num} and
{self.max_num}.")
except ValueError:
print("Please enter a valid number.")

if __name__ == "__main__":
game = GuessTheNumberGame()
game.play()

You might also like