Question: In the guess-the-number program discussed in this chapter, the computer program thinks of a number and the user inputs guesses, until a correct guess is

In the guess-the-number program discussed in this chapter, the computer program thinks of a number and the user inputs guesses, until a correct guess is detected. When we run the starter code the user needs to guess any number and then the computer program will let you know whether it's too low or high.

Use the starter file as a reference for your solution

In the numberguess.py file, complete the following:

  1. Write a program in which these roles are reversed: the user thinks of a number and the computer computes and outputs guesses.
  2. Like the computer program in the earlier version of this game (starter file), the user must provide hints.

    • < and > (meaning my number is smaller and my number is larger, respectively), when the computer guesses incorrectly.
    • The user inputs = when the computer guesses correctly.
    • The user should input the lower bound and the upper bound of the range of numbers at startup.
  3. The computer program should need at most round(math.log(high - low + 1, 2)) guesses to get the correct number.

    • Import the math module to use mathematical functions
  4. Your program should track the number of guesses.
    • Output the message Youre cheating! if the number of incorrect guesses reaches the maximum needed.
    • When the program guesses correctly, output the message Hooray, I've got it in x tries!, where x is the number of tries.

Here is a sample interaction with this program:

Enter the smaller number: 1 Enter the larger number: 100 Your number is 50 Enter =, <, or >: > Your number is 75 Enter =, <, or >: < Your number is 62 Enter =, <, or >: < Your number is 56 Enter =, <, or >: = Hooray, I've got it in 4 tries!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!