Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program to guess a number from 0 to 999, to the following specifications. Tell the user to think of a number from 0

Write a program to guess a number from 0 to 999, to the following specifications.

Tell the user to think of a number from 0 to 999. Note that the computer is guessing the number, the user just thinking of one.

Always start with a lower bound of 0 and an upper bound of 999.

As long as the user inputs a string starting with 'l' or 'm'

The next guess is always the average of the upper bound and the lower bound, so the first guess is always 499.

Prompt the user in the following format (changing 499 to another guess each time) and then input a string.

Is it 499? (Enter y if it is, l if your number is less, or m if your number is more.) 

As long as the user does not enter 'y', 'l' or 'm', prompt them with Enter y, lowercase L, or m only! Try again: and input another string.

If the user inputs a string starting with 'l' (or 'L'), change the upper bound.

If the user inputs a string starting with 'm' (or 'M'), change the lower bound.

The next guess is the average of the upper bound and the lower bound.

Keep count of the number of guesses.

Report the number of guesses made and the correct guess.

Sample run (user input in red):

Think of a number from 0 to 999. Is it 499? (Enter y if it is, l if your number is less, or m if your number is more.) n Enter y, lowercase L, or m only! Try again: l Is it 249? (Enter y if it is, l if your number is less, or m if your number is more.) m Is it 374? (Enter y if it is, l if your number is less, or m if your number is more.) l Is it 311? (Enter y if it is, l if your number is less, or m if your number is more.) y I guessed your number, 311 in 4 guesses. 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 2 Lnai 9285

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Joao Gama ,Alipio Jorge ,Carlos Soares

1st Edition

3319235249, 978-3319235240

More Books

Students also viewed these Databases questions

Question

What about leadership lessons from particularly good or bad bosses?

Answered: 1 week ago

Question

How would you assess the value of an approach like this?

Answered: 1 week ago