In: Computer Science
Think a Number Bob and Alice play a game in which Bob gives Alice a challenge to think of any number M between 1 to N. Bob then tells Alice a number X. Alice has to confirm whether X is greater or smaller than number M or equal to number M. This continues till Bob finds the number correctly. Your task is to find the maximum number of attempts Bob needs to guess the number thought of by Alice
import random
def numberGame():
#bob being the computer think of a random number between 1 to 108 as per question specification
n = random.randint(1, 108)
attempts = 0
#alice being u guess the number
x = int(input("X: "))
#untill entered number and the gueses number are equal alice keep guessing
while x != n:
#if n is greater then the current guessed number inform alice
if x > n:
print("lesser!")
elif x < n:
print("greater!")
else:
print("Number is ", x)
break
#keep counting the number of attempts
attempts += 1;
#guess next number
x = int(input("try again :"))
print("Number of attempts: ", attempts)
def main():
numberGame()
if __name__ == "__main__":
main()
please don't forget to upvote.Thankyou