forked from sumedha3111/Python-for-Beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python guessing game
67 lines (32 loc) · 1.08 KB
/
Python guessing game
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import random
import math
# Taking Inputs
lower = int(input("Emter Lower bound:- "))
# Taking Inputs
upper = int(input("Enter Upper bound:- "))
# generating random number between
# the lower and upper
x = random.randint(lower, upper)
print("\n\tYou've only ", round(math.log(upper - lower + 1, 2))," chances to guess the integer!\n")
# Initializing the number of guesses.
count = 0
# for calculation of minimum number of
# guesses depends upon range
while count < math.log(upper - lower + 1, 2):
count += 1
# taking guessing number as input
guess = int(input("Guess a number:- "))
# Condition testing
if x == guess:
print("Congratulations you did it in ", count, " try")
# Once guessed, loop will break
break
elif x > guess:
print("You guessed too small!")
elif x < guess:
print("You Guessed too high!")
# If Guessing is more than required guesses,
# shows this output.
if count >= math.log(upper - lower + 1, 2):
print("\nThe number is %d"%x)
print("\tBetter Luck Next time!")