Question

In: Computer Science

Please add to this Python, Guess My Number Program. Add code to the program to make...

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()   

Solutions

Expert Solution

# 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()


Related Solutions

(Python Code please) Guess the number! You will add to the program you created last week....
(Python Code please) Guess the number! You will add to the program you created last week. This week you will add quite a bit of code to your project. You will add an option for the computer to guess as well as the user to guess a number. In addition, you will add a menu system. Be sure to import random at the beginning of your code and use a comment block explaining what your program does #Guess the number...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program that comes up with a random number and the player has to guess it. The program output can be like this: I am thinking of a number between 1 and 20. Take a guess. 10 Your guess is too low. Take a guess. 15 Your guess is too low. Take a guess. 17 Your guess is too high. Take a guess. 16 Good job!...
lets say i make a PYTHON code that lets a user guess a number between 1-1000,...
lets say i make a PYTHON code that lets a user guess a number between 1-1000, every failed attempt you get a hint (go lower or go higher) how can i penalize the user if they dont follow the hints, example ( go higher!... your next pick: a smaller number... 2 you loose $100 for not following hint) 2 and the user has unlimited attempts, until he guesses the number, this is made using random.randint(1,1000) function THE ONLY THING IM...
Python programming: can someone please fix my code to get it to work correctly? The program...
Python programming: can someone please fix my code to get it to work correctly? The program should print "car already started" if you try to start the car twice. And, should print "Car is already stopped" if you try to stop the car twice. Please add comments to explain why my code isn't working. Thanks! # Program goals: # To simulate a car game. Focus is to build the engine for this game. # When we run the program, it...
Please make a Python program where the computer chooses a number and the player guesses the...
Please make a Python program where the computer chooses a number and the player guesses the number. You need an input, a random number, if statement, and a while loop. Please reference these when doing the code: ((random reference)) •>>> import random •>>> random.randint(1, 100) •67 ((While & if referefence)) •>>> cnum = 3 •>>>while true •>>> if cnum == 3: •>>> print(‘correct’) •>>> break •>>> elif cnum == 2: •>>> print(‘incorrect’) Please post the Python code and the screen...
In python make a simple code. You are writing a code for a program that converts...
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message....
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i...
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i used random.randint(1,1000) -must give hints like (pick a lower/higher number) which i already did -tell the user when he has repeated a number (help) -the game only ends when you guess the number (help) -you loose $50 every failed attempt (done) ****I did it as a while loop -
Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if...
please answer this in a simple python code 1. Write a Python program to construct the...
please answer this in a simple python code 1. Write a Python program to construct the following pattern (with alphabets in the reverse order). It will print the following if input is 5 that is, print z one time, y two times … v five times. The maximum value of n is 26. z yy xxx wwww vvvvvv
Working with Python. I am trying to make my code go through each subject in my...
Working with Python. I am trying to make my code go through each subject in my sample size and request something about it. For example, I have my code request from the user to input a sample size N. If I said sample size was 5 for example, I want the code to ask the user the following: "Enter age of subject 1" "Enter age of subject 2" "Enter age of subject 3" "Enter age of subject 4" "Enter age...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT