Skip to content

Commit 99bf616

Browse files
authored
Merge pull request The-Streamliners#46 from Akash2172/patch-3
Create Number guessing game.py
2 parents 28a6550 + a737e1c commit 99bf616

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Mathematical/Number guessing game.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import random
2+
import math
3+
# Taking Inputs
4+
lower = int(input("Emter 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 ", round(math.log(upper - lower + 1, 2))," chances to guess the integer!\n")
13+
14+
# Initializing the number of guesses.
15+
count = 0
16+
17+
# for calculation of minimum number of
18+
# guesses depends upon range
19+
while count < math.log(upper - lower + 1, 2):
20+
count += 1
21+
22+
# taking guessing number as input
23+
guess = int(input("Guess a number:- "))
24+
25+
# Condition testing
26+
if x == guess:
27+
print("Congratulations you did it in ", count, " try")
28+
# Once guessed, loop will break
29+
break
30+
elif x > guess:
31+
print("You guessed too small!")
32+
elif x < guess:
33+
print("You Guessed too high!")
34+
35+
# If Guessing is more than required guesses,
36+
# shows this output.
37+
if count >= math.log(upper - lower + 1, 2):
38+
print("\nThe number is %d"%x)
39+
print("\tBetter Luck Next time!")

0 commit comments

Comments
 (0)