-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGuessing_Game_With_Computer.py
37 lines (32 loc) · 1.39 KB
/
Guessing_Game_With_Computer.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
# Import random module
import random
# Function for Computer Guess
def comp_guess(low_val, high_val, randnum, count=0):
if high_val >= low_val: # When high_val greater than low_val
guess = low_val+(high_val-low_val)//2
if guess == randnum: # if guess is equals randnum then return the count
return count
elif guess > randnum: # if guess is greater than randnum the increase the count and call the function recursively again
count += 1
return comp_guess(low_val, guess-1, randnum, count)
else:
count += 1
return comp_guess(guess+1, high_val, randnum, count)
# Driver Function
randnum = random.randint(1, 100) # Store any random number b/w 1 and 100
count = 0 # Initialize count to 0
guess = 0 # Initialize guess to 0
while guess != randnum: # Loop till we guess the number
# Taking input from the user
guess = int(input("\nPlease Guess the No. b/w 1 & 100: "))
if guess > randnum: # Check whether number is Greater or not.
print("You Guess Higher No.")
elif guess < randnum: # Check whether number is Lower or not.
print("You Guess Lower No.")
else:
print("Great!, You guess the No.")
count += 1 # Increase count
# Display the Final Result
print(f"You took: {count} Chances!")
# Call comp_guess function
print(f"Computer took: {comp_guess(0,100,randnum)} Chances!")