Skip to content

Commit db8bce4

Browse files
committed
take the game to a higher stage
1 parent b49bae4 commit db8bce4

File tree

1 file changed

+100
-26
lines changed

1 file changed

+100
-26
lines changed

Guessing_Game.py

+100-26
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,105 @@
11
from random import randint
22
from time import sleep
33

4-
print("Hello Welcome To The Guess Game!")
5-
sleep(1)
6-
print("I\'m Geek! What's Your Name?")
7-
name = input()
8-
sleep(1)
9-
print(f"Okay {name} Let's Begin The Guessing Game!")
10-
a = comGuess = randint(0, 100) # a and comGuess is initialised with a random number between 0 and 100
11-
count = 0
12-
while True: # loop will run until encountered with the break statement(user enters the right answer)
13-
userGuess = int(input("Enter your guessed no. b/w 0-100:")) # user input for guessing the number
4+
def guessing_game(GUESS_RANGE, GUESS_LIMIT):
5+
# Set the initial values.
6+
RANDOM = randint(1, GUESS_RANGE)
7+
GUESS = int(input('What is your guess? '))
8+
ATTEMPTS_ALLOWED = GUESS_LIMIT
9+
done = False
1410

15-
if userGuess < comGuess: # if number guessed by user is lesser than the random number than the user is told to guess higher and the random number comGuess is changed to a new random number between a and 100
16-
print("Guess Higher")
17-
comGuess = randint(a, 100)
18-
a += 1
19-
count = 1
20-
21-
elif userGuess > comGuess: # if number guessed by user is greater than the random number than the user is told to guess lower and the random number comGuess is changed to a new random number between 0 and a
22-
print("Guess Lower")
23-
comGuess = randint(0, a)
24-
a += 1
25-
count = 1
26-
27-
elif userGuess == comGuess and count == 0: # the player needs a special reward for perfect guess in the first try ;-)
28-
print("Bravo! Legendary Guess!")
11+
# Validate the inputted guess.
12+
GUESS = InputValidation(GUESS, GUESS_RANGE)
13+
14+
# Now we have a valid guess.
15+
while GUESS_LIMIT > 0 and not done:
16+
GUESS_LIMIT -= 1 # Take one guess = lose one chance
17+
if GUESS_LIMIT > 0:
18+
if GUESS < RANDOM:
19+
print(f'It should be higher than {GUESS}.')
20+
elif GUESS > RANDOM:
21+
print(f'It should be lower than {GUESS}.')
22+
else:
23+
ATTEMPTS_TOOK = ATTEMPTS_ALLOWED - GUESS_LIMIT
24+
print(f'You nailed it! And it only took you {ATTEMPTS_TOOK} attempts.')
25+
done = True
26+
if GUESS_LIMIT > 0 and not done:
27+
print(f'You still have {GUESS_LIMIT} chances left.\n')
28+
GUESS = int(input('Try a new guess: '))
29+
# Another input validation loop.
30+
GUESS = InputValidation(GUESS, GUESS_RANGE)
31+
elif GUESS_LIMIT == 0 and not done: # Last chance to guess
32+
if GUESS == RANDOM:
33+
print(f'You nailed it! However, it took you all the {ATTEMPTS_ALLOWED} attempts.')
34+
else:
35+
print(
36+
f'GAME OVER! It took you more than {ATTEMPTS_ALLOWED} attempts. '
37+
f'The correct number is {RANDOM}.'
38+
)
39+
40+
41+
def InputValidation(GUESS, GUESS_RANGE):
42+
while not 1 <= GUESS <= GUESS_RANGE:
43+
print('TRY AGAIN! Your guess is out of range!\n')
44+
GUESS = int(input('What is your guess? '))
45+
return GUESS
46+
47+
48+
def easy():
49+
print('You are to guess a number between 1 and 10 in no more than 6 attempts.')
50+
guessing_game(10, 6)
51+
52+
53+
def medium():
54+
print('You are to guess a number between 1 and 20 in no more than 4 attempts.')
55+
guessing_game(20, 4)
56+
57+
58+
def hard():
59+
print('You are to guess a number between 1 and 50 in no more than 3 attempts.')
60+
guessing_game(50, 3)
61+
62+
63+
def try_again():
64+
print()
65+
again = input('Do you want to play again? (yes/no) ')
66+
if again.lower() in ['y', 'yes']:
67+
welcome()
68+
elif again.lower() in ['n', 'no']:
69+
print('Thanks for playing the game')
70+
else:
71+
print('INVALID VALUE')
72+
try_again()
73+
2974

30-
else: #Well, A Congratulations Message For Guessing Correctly!
31-
print("Congratulations, You Guessed It Correctly!")
75+
def welcome():
76+
print('Hello, Welcome to the Guessing Game!')
77+
name = input('I\'m Geek! What\'s Your Name? ')
78+
sleep(1)
79+
80+
print(f'Okay, {name}. Let\'s Begin The Guessing Game!')
81+
print('Choose a level:',
82+
'1. Easy',
83+
'2. Medium',
84+
'3. Hard',
85+
sep = '\n',
86+
)
87+
sleep(1)
88+
level = int(input('Pick a number: '))
89+
print()
90+
sleep(1)
91+
if level == 1:
92+
easy()
93+
try_again()
94+
elif level == 2:
95+
medium()
96+
try_again()
97+
elif level == 3:
98+
hard()
99+
try_again()
100+
else:
101+
print('INVALID VALUE! Please try again.\n')
102+
welcome()
103+
104+
105+
welcome()

0 commit comments

Comments
 (0)