-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberGuessingGame.py
38 lines (30 loc) · 995 Bytes
/
NumberGuessingGame.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
34
35
36
37
38
import math
import random
def main():
lower = int(input("Enter Lower bound: "))
upper = int(input("Enter Upper bound: "))
x = random.randint(lower, upper)
print("\n\tYou've only ",
round(math.log(upper - lower + 1,2)),
"chances to guess the integer! \n")
count = 0
while count < math.log(upper - lower + 1,2):
count += 1
guess = int(input("Guess a number: "))
if x == guess:
print("Congratulations you did it in ",
count, " try")
break
elif x > guess:
print("You guessed to small!")
elif x < guess:
print("You guessed too high!")
if count >= math.log(upper - lower + 1,2):
print("\nThe number is %d" % x)
print("\tBetter Luck Next Time!")
def loop():
again = "y"
while again == "y":
main()
again = input("Enter 'y' to play again or 'n' to quit: ")
loop()