-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber_Guessing_game.py
33 lines (31 loc) · 1.06 KB
/
Number_Guessing_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import random
import math
#Taking Inputs
lower = int(input("Enter Lower Bound:- "))
upper = int(input("Enter Upper Bound:- "))
#Generating random number between the lower and the upper
x = random.randint(lower,upper)
total_chances = math.ceil(math.log(upper - lower + 1, 2))
print("\n\tYou've only ", total_chances, " chances to guess the integer!\n")
#Initializing the number of guesses
count = 0
flag = False
#for caluulation of minimum number of guesses depending on range
while count < total_chances:
count += 1
#taking guesses as the input number
guess = int(input("Guess a number:- "))
#condition testing
if x == guess:
print("Congratulations you did it in ", count, " try")
#once guessed, break the loop
flag = True
break
elif x > guess:
print("You guessed to small!")
elif x< guess:
print("You guessed too high!")
#if guessing is more than required guesses, shows this output
if not flag:
print("\nThe number is %d" % x)
print("\tBetter Luck Next Time!")