|
| 1 | +import random |
| 2 | +import math |
| 3 | +# Taking Inputs |
| 4 | +lower = int(input("Enter Lower bound:- ")) |
| 5 | + |
| 6 | +# Taking Inputs |
| 7 | +upper = int(input("Enter Upper bound:- ")) |
| 8 | + |
| 9 | +# generating random number between |
| 10 | +# the lower and upper |
| 11 | +x = random.randint(lower, upper) |
| 12 | +print("\n\tYou've only ", |
| 13 | + round(math.log(upper - lower + 1, 2)), |
| 14 | + " chances to guess the integer!\n") |
| 15 | + |
| 16 | +# Initializing the number of guesses. |
| 17 | +count = 0 |
| 18 | + |
| 19 | +# for calculation of minimum number of |
| 20 | +# guesses depends upon range |
| 21 | +while count < math.log(upper - lower + 1, 2): |
| 22 | + count += 1 |
| 23 | + |
| 24 | + # taking guessing number as input |
| 25 | + guess = int(input("Guess a number:- ")) |
| 26 | + |
| 27 | + # Condition testing |
| 28 | + if x == guess: |
| 29 | + print("Congratulations you did it in ", |
| 30 | + count, " try") |
| 31 | + # Once guessed, loop will break |
| 32 | + break |
| 33 | + elif x > guess: |
| 34 | + print("You guessed too small!") |
| 35 | + elif x < guess: |
| 36 | + print("You Guessed too high!") |
| 37 | + |
| 38 | +# If Guessing is more than required guesses, |
| 39 | +# shows this output. |
| 40 | +if count >= math.log(upper - lower + 1, 2): |
| 41 | + print("\nThe number is %d" % x) |
| 42 | + print("\tBetter Luck Next time!") |
| 43 | + |
| 44 | +# Better to use This source Code on pycharm! |
0 commit comments