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 6a81ca8

Browse files
authored
Update Guessing_Game_With_Computer.py
1 parent 224e263 commit 6a81ca8

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed
Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
1+
# Import random module
12
import random
23

4+
# Function for Computer Guess
35

4-
def compguess(lowval, highval, randnum, count=0):
5-
if highval >= lowval:
6-
guess = lowval+(highval-lowval)//2
7-
if guess == randnum:
6+
7+
def comp_guess(low_val, high_val, randnum, count=0):
8+
if high_val >= low_val: # When high_val greater than low_val
9+
guess = low_val+(high_val-low_val)//2
10+
if guess == randnum: # if guess is equals randnum then return the count
811
return count
9-
elif guess > randnum:
12+
elif guess > randnum:# if guess is greater than randnum the increase the count and call the function recursively again
1013
count += 1
11-
return compguess(lowval, guess-1, randnum, count)
14+
return comp_guess(low_val, guess-1, randnum, count)
1215
else:
1316
count += 1
14-
return compguess(guess+1, highval, randnum, count)
17+
return comp_guess(guess+1, high_val, randnum, count)
1518

1619

17-
randnum = random.randint(1, 100)
18-
count = 0
19-
guess = 0
20-
print("\n\n\t\t\tWelcome To The GUESSING GAME Developed by @Satyam Tripathi")
21-
while guess != randnum:
20+
# Driver Function
21+
randnum = random.randint(1, 100) # Store any random number b/w 1 and 100
22+
count = 0 # Initialize count to 0
23+
guess = 0 # Initialize guess to 0
24+
while guess != randnum: # Loop till we guess the number
25+
# Taking input from the user
2226
guess = int(input("\nPlease Guess the No. b/w 1 & 100: "))
23-
if guess > randnum:
24-
print("You Guess \"Higher No.\" !")
25-
elif guess < randnum:
26-
print("You Guess \"Lower No.\" !")
27+
if guess > randnum:# Check whether number is Greater or not.
28+
print("You Guess Higher No.")
29+
elif guess < randnum:# Check whether number is Lower or not.
30+
print("You Guess Lower No.")
2731
else:
28-
print("Greate!, You guess the No. $$$")
29-
count += 1
30-
print(f"You took: {count} Chances !")
31-
print(f"Computer took: {compguess(0,100,randnum)} Chances !")
32+
print("Great!, You guess the No.")
33+
count += 1 # Increase count
34+
# Display the Final Result
35+
print(f"You took: {count} Chances!")
36+
# Call comp_guess function
37+
print(f"Computer took: {comp_guess(0,100,randnum)} Chances!")

0 commit comments

Comments
(0)

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