Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ \"Write a program that will guess an integer that the user has picked. Im

ID: 3784642 • Letter: I

Question

In C++

"Write a program that will guess an integer that the user has picked. Imagine that the user will write down a positive integer x on a piece of paper and your program will repeatedly ask questions in order to guess what x is, and the user replies honestly. Your program will start by asking for an int n, and you must have 1 x n. After that, the program will successively guess what x is, and the user must tell the computer if x is equal to the guess (entering ’e’), larger than the guess (entering ’l’), or smaller than the guess (entering ’s’).

Your program will guess by maintaining a lower bound (initially 1) and upper bound (initially n) and pick the largest integer equal to or smaller than1 the midpoint of the lower bound and upper bound. If the user responds with ’l’ indicating that x is larger, the guess becomes the new lower bound plus one. If the user responds with ’s’ indicating that x is smaller, the guess becomes the new upper bound minus one. If the user responds with ’e’ indicating that x is the guess, your program will report the number of guesses made and terminate execution: "

Example of how program should look like:

Enter n: 50

Is your number 25? l

Is your number 38? l

Explanation / Answer

import random

3.

4. guessesTaken = 0

5.

6. print('Hello! What is your name?')

7. myName = input()

8.

9. number = random.randint(1, 20)

10. print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')

11.

12. while guessesTaken < 6:

13.     print('Take a guess.') # There are four spaces in front of print.

14.     guess = input()

15.     guess = int(guess)

16.

17.     guessesTaken = guessesTaken + 1

18.

19.     if guess < number:

20.         print('Your guess is too low.') # There are eight spaces in front of print.

21.

22.     if guess > number:

23.         print('Your guess is too high.')

24.

25.     if guess == number:

26.         break

27.

28. if guess == number:

29.     guessesTaken = str(guessesTaken)

30.     print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')

31.

32. if guess != number:

33.     number = str(number)

34.     print('Nope. The number I was thinking of was ' + number)