Skip to content

Commit f135de7

Browse files
committed
New Number Guessing Game without GUI
1 parent dec25a4 commit f135de7

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Number Guessing Game/main.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import random
2+
3+
"""
4+
Funtion start() create the magic number, number of attemps, and ask user for input
5+
"""
6+
def start():
7+
global magic_number
8+
magic_number = random.randint(1, 100)
9+
10+
global attempts
11+
attempts = 0
12+
13+
print('---------------------------')
14+
print('Guess a number between 1 and 100')
15+
16+
# Function that checks if player won, if it won, returns True
17+
def check_win(player_guess):
18+
if player_guess > magic_number:
19+
print('Too big...')
20+
elif player_guess < magic_number:
21+
print('Too small')
22+
elif player_guess == magic_number:
23+
return True
24+
25+
start()
26+
27+
# Game loop
28+
while True:
29+
# Take the player input
30+
guess = int(input())
31+
attempts += 1
32+
33+
if check_win(guess):
34+
print('You won! - Number of attempts: ' + str(attempts))
35+
36+
keep_playing = input('Keep playing?(y\\n)')
37+
# If player want to keep the game, reset the number of attempts
38+
if keep_playing == 'y':
39+
attempts = 0
40+
start()
41+
# If player don't want to keep playing, quit the game
42+
elif keep_playing == 'n':
43+
quit()

0 commit comments

Comments
 (0)