Skip to content

Commit a220125

Browse files
Merge pull request #22 from sakshimuttha578/add-mystiguess
Added a number guess game - MystiGuess
2 parents 13e0479 + f47bdf8 commit a220125

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

programs/mysti_guess.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Generate a random num
2+
import random
3+
4+
# Specifying the range bt taking input
5+
print('Please enter the range of numbers -')
6+
min = int(input('Minimum : '))
7+
max = int(input('Maximum : '))
8+
num = random.randint(min,max)
9+
10+
# attempts
11+
attempts = 5
12+
guessed = 0
13+
14+
# loop
15+
while True :
16+
# take input guess
17+
try:
18+
print(f'You have only {attempts} attempts!')
19+
guess = int(input(f'Guess the number between {min} and {max} : '))
20+
21+
# if num < guess : too low
22+
if guess < num :
23+
print('Too low!')
24+
guessed += 1
25+
attempts -= 1
26+
if attempts == 0:
27+
print('Attempts Over!')
28+
print('Sorry! Try Next Time!')
29+
print(f'The number was {num}')
30+
break
31+
32+
# if num > guess : too high
33+
elif guess > num :
34+
print('Too high!')
35+
guessed += 1
36+
attempts -= 1
37+
if attempts == 0:
38+
print('Attempts Over!')
39+
print('Sorry! Try Next Time!')
40+
print(f'The number was {num}.')
41+
break
42+
43+
# else num == guess : well done
44+
else :
45+
guessed += 1
46+
print(f'Congratulations! You guessed the number in {guessed} attempts!')
47+
break
48+
49+
# if invalid num : error
50+
except ValueError:
51+
print('Please enter a valid number')

0 commit comments

Comments
 (0)