Skip to content

Commit 1df207f

Browse files
author
codehouseindia
authored
Merge pull request codehouseindia#480 from SA888-PM/master
Added the number guessing
2 parents 64dd335 + c1c6f67 commit 1df207f

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

guess_number.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# https://m.facebook.com/story.php?story_fbid=101362998411817&id=100056143503621
2+
# subscribed by codehouseindia
3+
4+
import random
5+
import math
6+
# Taking Inputs
7+
lower = int(input("Emter Lower bound:- "))
8+
9+
# Taking Inputs
10+
upper = int(input("Enter Upper bound:- "))
11+
12+
# generating random number between
13+
# the lower and upper
14+
x = random.randint(lower, upper)
15+
print("\n\tYou've only ", round(math.log(upper - lower + 1, 2))," chances to guess the integer!\n")
16+
17+
# Initializing the number of guesses.
18+
count = 0
19+
20+
# for calculation of minimum number of
21+
# guesses depends upon range
22+
while count < math.log(upper - lower + 1, 2):
23+
count += 1
24+
25+
# taking guessing number as input
26+
guess = int(input("Guess a number:- "))
27+
28+
# Condition testing
29+
if x == guess:
30+
print("Congratulations you did it in ", 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!")

0 commit comments

Comments
 (0)