In: Computer Science
Please add to this Python, Guess My Number Program. Add code to the program to make it ask the user for his/her name and then greet that user by their name. Please add code comments throughout the rest of the program If possible or where you add in the code to make it ask the name and greet them before the program begins.
import random
def menu():
print("\n\n1. You guess the number\n2. You type a number and see if
the computer can guess it\n3. Exit")
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:
c=int(input("Enter your choice: "))
if(c>=1 and c<=3):
return c
else:
print("Enter number between 1 and 3 inclusive.")
except:
#print exception
print("Numbers Only!")
def guessingGame():
min_number=1
max_number=10
#set number of guesses = 0
numGuesses=0
guessed_numbers = [] # list of guessed numbers
rand=random.randint(min_number, max_number)
#prints the header, welcoming the user
#print("\nWelcome to the Guess My Number Program!")
#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:
guess=eval(input("Please try to guess my number between 1 and
10:"))
guessed_numbers.append(guess)
#check if the guess is less than 0, then continye to beginning of
the loop
if(guess<0):
continue;
elif (guess==rand):
#increment the guess count by 1
numGuesses=numGuesses+1
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
elif(guess < rand):
#increment the guess count by 1
numGuesses = numGuesses +1
print("Too low")
else:
#increment the guess count by 1
numGuesses = numGuesses + 1
print("Too high")
except:
#print exception
print("Numbers only!")
def guessingGameComp():
countGuess=0
guessed_numbers = [] # list of guessed numbers
userNumber=int(input("\nPlease enter a number between 1 and 10 for
the computer to guess:"))
while userNumber<1 or userNumber>10:
userNumber=int(input("Guess a number between 1 and 10: "))
while True:
countGuess+=1
compRand = random.randint(1,10)
guessed_numbers.append(compRand)
if(userNumber==compRand):
print("The computer guessed it! It took {}
attempts".format(countGuess))
print("The computer guessed the following numbers:
"+str(guessed_numbers))
break
elif(compRand<userNumber):
print("The computer guessed {} which is too
low".format(compRand))
else:
print("The computer guessed {} which is too
high".format(compRand))
def main():
print("Welcome to my Guess the number program!")
while True:
userChoice=menu()
if userChoice==1:
guessingGame()
elif userChoice==2:
guessingGameComp()
elif userChoice==3:
print("\nThank you for playing the guess the number game!")
break
else:
print("Invalid choice!!!")
#call the main function
main()
# Python Code is modified to discard duplicate guesses by the computer
import random
def menu(): #function for getting the user input on what he wants
to do
print("\n\n1. You guess the number\n2. You type a
number and see if the computer can guess it\n3. Exit")
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:
c=int(input("Enter your choice: "))
if(c>=1 and
c<=3):
return c
else:
print("Enter number between 1 and 3
inclusive.")
except:
#print
exception
print("Numbers
Only!")
def guessingGame(): #user guesses the number generated randomly by
the computer
min_number=1
max_number=10
#set number of guesses = 0
numGuesses=0
guessed_numbers = [] # list of guessed numbers
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:
guess=eval(input("Please try to guess my number between 1 and
10:"))
guessed_numbers.append(guess)
#check if the
guess is less than 0, then continue to beginning of the loop
if(guess<0):
continue;
elif
(guess==rand):
#increment the guess count by 1
numGuesses=numGuesses+1
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
elif(guess <
rand):
#increment the guess count by 1
numGuesses = numGuesses +1
print("Too low")
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):
countGuess = countGuess - 1;
print("\n Already guessed number: ", compRand)
#remove this line of code if you don't want to show already guessed
numbers.
continue
guessed_numbers.append(compRand) #add valid guessed number to
guessed_numbers list
if(userNumber==compRand): #if number guessed by computer is
correct, break out of loop.
print("\nThe computer guessed it! It took {}
attempts".format(countGuess))
print("\nThe computer guessed the following
numbers: "+str(guessed_numbers))
break
elif(compRand<userNumber):#if number gueesed by computer is less
than user input
print("\nThe computer guessed {} which is too
low".format(compRand))
else: #if number
gueesed by computer is higher than user input
print("\nThe computer guessed {} which is too
high".format(compRand))
break
def main():
print("Welcome to my Guess the number program!")
name = input("What's your name: ") #taking name of
user as input
print("Hi ",name,) #greeting user
while True:
userChoice=menu()
if userChoice==1:
guessingGame()
elif userChoice==2:
guessingGameComp()
elif userChoice==3:
print("\nThanks", name, "for playing the guess the number game!")
#greeting user after the game ended.
break
else:
print("Invalid
choice!!!")
#call the main function
main()