forked from souravjain540/Basic-Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber-Guessing-Game.py
68 lines (42 loc) · 1.88 KB
/
Number-Guessing-Game.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#Program for Game to Guess a number
#Intro Code to be displayed at the beginning of the game
def Intro():
print("--------------------------------------------------------------------------------------------------------------------")
print("Hi and Welcome to Number Guesser!!!")
print("In your mind choose any number between 1 and 1000")
print("When the programme displays a number you have will have three options:")
print("1.Greater\n2.Smaller\n3.Equal")
print("Give the correct response and your point will be the number of tries the computer takes to guess your number")
print("Let's Go Then")
print("--------------------------------------------------------------------------------------------------------------------")
Ask()
#Asking the user to start a round
def Ask():
c=input(("Are you ready to begin [Y/N] "))
if c=='Y' or c=='y' :
startgame()
else:
Ask()
#Game Code using binary search
def startgame():
low , high = 1, 1000
mid = low + (high-low)//2 #integer division
score = 1
while(low<=high):
mid = low + (high-low)//2
print("Choose ",mid," is :\n1. Greater\n2. Smaller\n3. Equal\n than your number")
Response = int(input())
if(Response==1): #Given number is greater than chossen number. So we limit search region between low and mid-1
score +=1
high = mid-1
elif(Response==2): #Given number is smaller than chossen number. So we limit search region between low+1 and mid
score += 1
low = mid+1
else: #Given number is equal to the chossen number. So we end the game and give the score
print("Your score is", score)
b=int(input("Would you like to :\n1.Play Again\n2.Exit\n"))
if(b==1):
Ask()
else:
return
Intro()