Question

In: Computer Science

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

  1. You guess the number
  2. You type a number and see if the computer can guess it
  3. Exit

Solutions

Expert Solution

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 :


  
  
  
  
  


Related Solutions

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...
Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random...
Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random number with min number being 1 and max being 10 import random min_number = 1 max_number = 10 rand = random.randint(min_number, max_number) #Prints the header, welcoming the user print("Welcome to the Guess My Number Program!") while (True): #While loop, comparing the users guessed number with the random number. #If it matches, it will say you guessed it. guess = eval(input("Please try to guess my...
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 -
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...
CODE IN PYTHON 1. Write a program that asks the user for the number of slices...
CODE IN PYTHON 1. Write a program that asks the user for the number of slices of pizza they want to order and displays the total number of dollar bills they owe given pizza costs 3 dollars a slice.  Note: You may print the value an integer value. 2. Assume that y, a and b have already been defined, display the value of x: x =   ya+b    3. The variable start_tees refers to the number of UD T-shirts at the start...
Please write in beginner level PYTHON code! Your job is to write a Python program that...
Please write in beginner level PYTHON code! Your job is to write a Python program that asks the user to make one of two choices: destruct or construct. - If the user chooses to destruct, prompt them for an alternade, and then output the 2 words from that alternade. - If the user chooses construct, prompt them for 2 words, and then output the alternade that would have produced those words. - You must enforce that the users enter real...
1. Please program the following in Python 3 code. 2. Please share your code. 3. Please...
1. Please program the following in Python 3 code. 2. Please share your code. 3. Please show all outputs. Instructions: Run Python code  List as Stack  and verify the following calculations; submit screen shots in a single file. Postfix Expression                Result 4 5 7 2 + - * = -16 3 4 + 2  * 7 / = 2 5 7 + 6 2 -  * = 48 4 2 3 5 1 - + * + = 18   List as Stack Code: """...
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....
Python This week you will write a program in Python that mimics an online shopping cart...
Python This week you will write a program in Python that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT