In: Computer Science
Can you please see what I have done wrong with my program code and explain, This python program is a guess my number program. I can not figure out what I have done wrong. When you enter a letter into the program, its supposed to say "Numbers Only" as a response. I can not seem to figure it out.. instead of an error message.
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:
c=int(input("Enter your choice: "))
if(c>=1 and c<=3):
return c
else:
print("Enter number between 1 and 3 inclusive.")
def guessingGame():
min_number=1
max_number=10
#set number of guesses = 0
numGuesses=0
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:"))
#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 ", numGuesses,"attempts")
#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
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)
if(userNumber==compRand):
print("The computer guessed it! It took {}
attempts".format(countGuess))
break
elif(userNumber<compRand):
print("The computer guessed {} which is too
low".format(compRand))
else:
print("The computer guessed {} which is too
high".format(compRand))
def main():
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!!!")
main()
#Python program for guessing game with user menu options. #The corrections are shown in bold letters #intendations must be followed in the python program. 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: #Correction1: #Using try-except for the choice will handle the non-numbers #If user enters alphabets, 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 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:")) #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 ", numGuesses,"attempts") #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 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) if(userNumber==compRand): print("The computer guessed it! It took {} attempts".format(countGuess)) break elif(userNumber<compRand): print("The computer guessed {} which is too low".format(compRand)) else: print("The computer guessed {} which is too high".format(compRand)) def main(): 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!!!") #calling main method to start the program main()
------------------------------------------------------------------------------------------
Screen-shots with indentations
Sample Output:
1. You guess the number
2. You type a number and see if the computer can guess it
3. Exit
Enter your choice: a
Numbers only!
Enter your choice: 1
Welcome to the Guess My Number Program!
Please try to guess my number between 1 and 10:5
Too low
Please try to guess my number between 1 and 10:6
Too low
Please try to guess my number between 1 and 10:7
You guessed it! It took you 3 attempts
1. You guess the number
2. You type a number and see if the computer can guess it
3. Exit
Enter your choice: 2
Please enter a number between 1 and 10 for the computer
to guess:9
The computer guessed 5 which is too high
The computer guessed 2 which is too high
The computer guessed it! It took 3 attempts
1. You guess the number
2. You type a number and see if the computer can guess it
3. Exit
Enter your choice: 3
Thank you for playing the guess the number
game!