-
Notifications
You must be signed in to change notification settings - Fork 4
/
game.py
56 lines (46 loc) · 1.35 KB
/
game.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import random
def welcome_message():
print('Welcome to "Guess a Number!"')
def choose_difficulty():
print('\nChoose difficulty:')
print('[0] Easy')
print('[1] Medium')
print('[2] Hard')
print('[3] Legend')
try:
choice = int(input('Your choice: '))
assert(choice >= 0 or choice <= 3)
return choice
except:
print('Please choose typing 1, 2 or 3.')
return choose_difficulty()
def get_max_tries(difficulty):
max_tries = 50 - (difficulty * 10)
if difficulty == 3:
max_tries = 1
return max_tries
def game():
welcome_message()
max_tries = get_max_tries(choose_difficulty())
number = random.randint(0, 100)
tries = 0
previous_numbers = []
done = False
print('Enter a guess between 0 and 100')
while not done:
tries += 1
if tries >= max_tries:
print('You lost!')
done = True
break
guess = int(input(f'{tries}/{max_tries}: {previous_numbers} > '))
if guess == number:
done = True
print('You won!')
else:
previous_numbers.append(guess)
if guess > number:
print('hint: the actual number is smaller')
else:
print('hint: the actual number is larger')
print(f'You got it after {tries} tries!')