Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b57f8e3

Browse files
Merge pull request #111 from Charm2316/patch-1
Create Number Guessing Game
2 parents 0e79530 + 2130336 commit b57f8e3

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

‎Number Guessing Game

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /