In: Computer Science
Explain this python program as if you were going to present it to a class in a power point presentation. How would you explain it? I am having a hard time with this. I have my outputs and code displayed throughout 9 slides.
#Guess My Number Program
# Python Code is modified to discard duplicate guesses by the
computer
import random
#function for getting the user input on what they want to do.
def menu():
#print the options
print("\n\n1. You guess the number\n2. You type a number and see if
the computer can guess it\n3. Exit")
#Loop until correct choice is entered.
while True:
#using try-except for the choice will handle the non-numbers
#if user enters letters, then except will show the message, Numbers
only!
try:
#Taking input and converting to integer.
c=int(input("Enter your choice: "))
# if input is correct, return number.
if(c>=1 and c<=3):
return c
#if input is not correct, print message and ask again.
else:
print("Enter number between 1 and 3 inclusive.")
#For non numeric input, throw exception.
except:
#print exception
print("Numbers Only!")
#This function is the main game for the player.
def guessingGame(): #user guesses the number generated randomly by
the computer
#defining minimum and maximum value.
min_number=1
max_number=10
#set number of guesses = 0
numGuesses=0
guessed_numbers = [] #list of guessed numbers
#Generating a random number between min and max which user has to
guess.
rand=random.randint(min_number, max_number)
#While loop, comparing the users guessed number with the random
number.
#If it matches, it will say you guessed it.
while (True):
#use try-block
try:
#Asking for input from the player.
guess=eval(input("Please try to guess my number between 1 and
10:"))
#Adding guess to list of guesses.
guessed_numbers.append(guess)
#check if the guess is less than 0, then continue to beginning of
the loop
if(guess<0):
continue;
#if guess is correct, print results.
elif (guess==rand):
#increment the guess count by 1
numGuesses=numGuesses+1
#Print results
print("You guessed it! It took you {}
attempts".format(numGuesses))
# print the guesses numbers list
print('You picked the following numbers:
'+str(guessed_numbers))
#break will end the loop once the guess is a match.
#Conditions if the guess is too low or too high to keep
guessing
break
#If guess is lower than actual then increasing guess count and
print low message.
elif(guess < rand):
#increment the guess count by 1
numGuesses = numGuesses +1
print("Too low")
#if guess is higher than actual then increasing guess count and
print low message.
else:
#increment the guess count by 1
numGuesses = numGuesses + 1
print("Too high")
except:
#print exception
print("Numbers only!")
# In guessingGameComp function, computer will guess the number
entered by the user. I have modified the code so that wherever
computer generates same random number again, it will not be taken
into account.
# For e.g., if initially computer guessed the number to be 3, and
again it tries to guess the number 3.So, this limitation is
removed
def guessingGameComp():
countGuess=0 #initially, number of guess attempts 0.
guessed_numbers = [] # list of guessed numbers
#taking input number from user
userNumber=int(input("\nPlease enter a number between 1 and 10 for
the computer to guess:"))
#execute below loop only when number entered by user is between 0
and 11.
while userNumber > 0 and userNumber < 11:
while True:
countGuess+=1 #counting the attempts by computer
compRand = random.randint(1,10) #random number guessed by
computer
#if compRand is already guesses, do not count this attempt
if(compRand in guessed_numbers):
#decreasing count by 1 because guess is already guessed.
countGuess = countGuess - 1;
#remove this line of code if you dont want to show already guessed
numbers.
print("\n Already guessed number: ", compRand)
continue
#add valid guessed number to guessed_numbers list.
guessed_numbers.append(compRand)
#if number guessed by computer is correct, break out of loop.
if(userNumber==compRand):
#if guess is correct, print list of guesses and count the
guesses.
print("\nThe computer guessed it! It took {}
attempts".format(countGuess))
print("\nThe computer guessed the following numbers:
"+str(guessed_numbers))
break
#if number guesses by computer is higher than user input
elif(compRand
print("\nThe computer guessed {} which is too
low".format(compRand))
#if number gueesed by computer is less than user input
else:
print("\nThe computer guessed {} which is too
high".format(compRand))
break
def main():
print("Welcome to my Guess the number program!") #Printing welcome
message
name = input("What's your name: ") #taking name of user as
input
print("Hello ",name,) #greeting user
while True:
#Show menu and take input from user.
userChoice=menu() #If user choice is 1, this means player will play
game.
if userChoice==1:
guessingGame()#If user choice is 2, this means computer will play
the game.
elif userChoice==2:
guessingGameComp()#If choice is 3, then user will exit the
game.
elif userChoice==3:
print("\nThanks", name, "for playing the guess the number game!")
#greeting user after the game ended.
break
else: #For invalid choice print error message.
print("Invalid choice!!!")
#call the main function
main()
This is the guessing game .
It has tow parts
1) Computer selects the random number and you have to guess that number
2) You are select the secret number and computer is guess that number
now this program is divided into three module.
1) main
2) Menu
3) guessingGame
4) guessingGameComp
Main Module
first it will print the greeting message
then it will ask user's name
Now it will call the Menu function for taking user choice what user want to do
based on the user choice it will do the rest
if user choose the option 1 then it will call the guessingGame fucntion
if user choose the option 2 then it will call the guessingGameComp
and if user will choose option 3 then it will print the last greeting message and program will terminate
Module Menu
this is the function named menu
it will print the menu with three option
1. You guess the number
2. You type a number and see if the computer can guess it
3. Exit"
and ask user to select the option .
if user select a number other then 1 to 3 then it will give an error
if user enter the any string or other then the int value then it will give an error for this i am using try catch block
and then user asked again to select the option
when user enter the valid option then this function will return the user choice to the main function.
guessingGame module
this function is called when user enter the 1 in menu module
in this function first numGuesses variable set to 0
and a random number between 1 to 10 is generated and stores int he variable called rand
then this function ask the user to guess the number between 1 to 10 and this number stored in variable called guess
then this user guessed number is add to the list named guessed_numbers
now it will check the guessed number is less then zero then asked again
otherwise there are three possibilites
1)
if it a positive number then it will compare with the rand number
then add 1 to the numGuesses
if user guessed number and rand number is both are equal then
then it will inform the user that he/she guessed the number in numGuesses attempt and print the list of guessed_numbers
2)
if guessed number is less then rand number then it will print too low and add 1 to the numGuesses
and again asked the user to guess another number
3)
if guessed number is greater then rand number then it will print too high and add 1 to the numGuesses
and again asked the user to guess another number
it also check user enter the number only if user enter the string or anything then it will give an error
this will continue untill user guess the true number
guessingGameComp module
in this countGuess var set to 0 it will track the number of guess comp required to guess the number
then initialize the guessed_numbers blank list it will hold all the non repeating guess of the computer
then it will ask user to enter a number between 1 to 10 ans stores in userNumber variable
if the number is between 1 and 10 then computer guess the number between 1 and 11 and stores it in compRand named variable
then this number is check to see if its preset in the guessed_numbers if it will preset then countGuess decremented by 1 and computer will guess another number
if this number is not present in the guessed_numbers list then there 3 posibilities again
1)
if compRand is equals to the userNumber then it will print the message the message comp guess in the countGuess number
and also print the guessed_numbers list
2)
if compRand is less then then userNumber then it will print the comp guess is low and it will increment the countGuess by 1
3)
if compRand is greater then then userNumber then it will print the comp guess is high and it will increment the countGuess by 1
this will continue untill computer will guess the number that is same as userguess
If you have any query regarding the answer please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.