Code:
import randomimport timecorrectanswer = random.randint(1,20)guesscount = 1print(“I will be selecting numbers between 1 and 20”)print(“Picking a number…”)time.sleep(2)guess = int(input(“Guess a number: “))while guess != correctanswer:guesscount += 1if guess < correctanswer:guess = int(input(“You need to make it higher. Try again: “))else:guess = int(input(“You need to make it lower. Try again: “))print(f”Congrats. The correct answer is {correctanswer}. It took you total of {guesscount} guesses.”)
Sample Output:
I will be selecting numbers between 1 and 20
Picking a number…
Guess a number: 17
You need to make it lower. Try again: 12
You need to make it higher. Try again: 13
You need to make it higher. Try again: 15
You need to make it higher. Try again: 16
Congrats. The correct answer is 16. It took you total of 5 guesses.
Picking a number…
Guess a number: 17
You need to make it lower. Try again: 12
You need to make it higher. Try again: 13
You need to make it higher. Try again: 15
You need to make it higher. Try again: 16
Congrats. The correct answer is 16. It took you total of 5 guesses.