Question: ---->(Python Programming) -----> Modify original program so that the user is asked to think of a secret number and the computer guesses that number. Here
---->(Python Programming)
-----> Modify original program so that the user is asked to think of a secret number and the computer guesses that number. Here is the interaction:
1.The computer asks the user for the range.
2.The user inputs the range and thinks of a secret number in the range.
3.The computer tries to guess the secret number in as little attempts as possible.
4.For each guess, the user should enter:
> if the secret number is greater than the guess;
< if the secret number is less than the guess; and
= if the secret number is equal to the guess.
5.With the above information, the computer adjusts the range and guesses again.
orginal program:
import random
def main(): smaller = int (input ("Enter the smaller number: ")) larger = int (input ("Enter the larger number: ")) myNumber = random.randint(smaller,larger) count = 0
while True: count += 1 userNumber = int (input ("Enter your Guess: ")) if userNumber < myNumber: print("Too small") elif userNumber > myNumber: print("Too large") else: print("You have got it in", count, "tries!") break
if __name__ == '__main__': main()
Sample run should be:
Enter the smaller number in the range: 0 Enter the larger number in the range: 100 Now think of a number between 0 and 100 . Hit enter to continue... I'm trying to guess your number. I know it is between 0 and 100 Is your number 50 ? Enter =, <, or >: < I'm trying to guess your number. I know it is between 0 and 49 Is your number 24 ? Enter =, <, or >: > I'm trying to guess your number. I know it is between 25 and 49 Is your number 37 ? Enter =, <, or >: < I'm trying to guess your number. I know it is between 25 and 36 Is your number 30 ? Enter =, <, or >: = Hooray, I've got it in 4 tries!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
