In: Computer Science
(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 week 6
#Name:
#Date:
#Menu system displays - ask user if they want to guess a number, have computer guess a number, or exit
#Random number, loop while true
#ask user for number. Check to see if the value is a number between 1 and 10
#if number is too high or too low, tell user, if they guessed it break out of loop
#ask user to enter a number, computer randomly guesses
Display "Welcome to my Guess the number program!"
while true
Display "1. You guess the number"
Display "2. You type a number and see if the computer can guess
it"
Display "3. Exit"
Get option
if(option ==1)
random mynumber
count=1
while True
try
Display "Guess a number between 1 and 10"
Get guess
while guess<1 or guess>10
Display "Guess a number between 1 and 10"
Get guess
except
Display "numbers only"
continue
if (guess<mynumber)
Display "Too low"
count=count+1
else if (guess>mynumber)
Display "Too high"
count=count+1
else if (guess==mynumber)
Display "You guessed it in "+ count + " attempts"
if(option ==2)
Get number from user
count=1
while True
Get randomval from computer
if (number<randomval)
Display "Too low"
count=count+1
else if (number>randomval)
Display "Too high"
count=count+1
else if (number==randomval)
Display "The computer guessed it in "+ count + " attempts. The number was "+randomval
else
break
When you run the program you should see something like the following:
Welcome to my Guess the number program!
Please find the program and output below.
import random
print("Welcome to my Guess the number program!\n".center(50))
print("*****************************************\n".center(50))
while (True):
print("\n\n1. You guess the number")
print("2. You type a number and see if the computer can guess
it")
print("3. Exit\n")
option= int(input())
if (option==1):
count=1
mynumber = random.randint(0, 10)
while (True):
try:
guess = int(input("Guess a number between 1 and 10 "))
except ValueError:
print("Please enter numbers only!")
continue
if (guess<mynumber):
print("Too low")
count=count+1
elif (guess>mynumber):
print("Too high")
count=count+1
elif (guess==mynumber):
print("You guessed it in {}".format(count) + " attempts")
break
elif (option==2):
count=1
try:
number = int(input("Guess a number between 1 and 10 "))
except ValueError:
print("Please enter numbers only!")
continue
while(True):
randomval=random.randint(0, 10)
if (number<randomval):
print("Too low")
count=count+1
elif (number>randomval):
print("Too high")
count=count+1
elif (number==randomval):
print("The computer guessed it in {}".format(count) + " attempts.
The number was {}".format(randomval))
break
else:
break
OUTPUT :