Use Bisection Search to Guess Numbers
Use Bisection Search to Guess Numbers
Guess numbers based on the algorithm of the bisection method. By using the bisection method, you can filter out half of the numbers that may be wrong each time, which increases your guessing accuracy.
import random
num = random.randint(1,100)
count = 7
print(“The number guessing game starts! You have seven chances.”)
while True:
guess = int(input(“Input the number you think:”))
if guess == num:
print(“You’re right! Congradulations!”)
break
elif guess > num:
count -= 1
print(“Smaller”)
print(“You have”, count, “chance(s)”)
elif guess < num:
count -= 1
print(“Greater”)
print(“You have”, count, “chance(s)”)
if count == 0:
print(“Sorry! Game Over!”)
print(“The number is”,num)
break