-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathmysti_guess.py
More file actions
51 lines (44 loc) · 1.33 KB
/
mysti_guess.py
File metadata and controls
51 lines (44 loc) · 1.33 KB
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
# Generate a random num
import random
# Specifying the range bt taking input
print('Please enter the range of numbers -')
min = int(input('Minimum : '))
max = int(input('Maximum : '))
num = random.randint(min,max)
# attempts
attempts = 5
guessed = 0
# loop
while True :
# take input guess
try:
print(f'You have only {attempts} attempts!')
guess = int(input(f'Guess the number between {min} and {max} : '))
# if num < guess : too low
if guess < num :
print('Too low!')
guessed += 1
attempts -= 1
if attempts == 0:
print('Attempts Over!')
print('Sorry! Try Next Time!')
print(f'The number was {num}')
break
# if num > guess : too high
elif guess > num :
print('Too high!')
guessed += 1
attempts -= 1
if attempts == 0:
print('Attempts Over!')
print('Sorry! Try Next Time!')
print(f'The number was {num}.')
break
# else num == guess : well done
else :
guessed += 1
print(f'Congratulations! You guessed the number in {guessed} attempts!')
break
# if invalid num : error
except ValueError:
print('Please enter a valid number')