forked from Shahrayar123/Python-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
43 lines (35 loc) · 1.08 KB
/
main.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
import random
"""
Funtion start() create the magic number, number of attemps, and ask user for input
"""
def start():
global magic_number
magic_number = random.randint(1, 100)
global attempts
attempts = 0
print('---------------------------')
print('Guess a number between 1 and 100')
# Function that checks if player won, if it won, returns True
def check_win(player_guess):
if player_guess > magic_number:
print('Too big...')
elif player_guess < magic_number:
print('Too small')
elif player_guess == magic_number:
return True
start()
# Game loop
while True:
# Take the player input
guess = int(input())
attempts += 1
if check_win(guess):
print('You won! - Number of attempts: ' + str(attempts))
keep_playing = input('Keep playing?(y\\n)')
# If player want to keep the game, reset the number of attempts
if keep_playing == 'y':
attempts = 0
start()
# If player don't want to keep playing, quit the game
elif keep_playing == 'n':
quit()