-
Notifications
You must be signed in to change notification settings - Fork 1
/
Profiling_code.py
68 lines (55 loc) · 2.02 KB
/
Profiling_code.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
57
58
59
60
61
62
63
64
65
66
67
68
# Number Guessing Game
# Date created: 2-09-2023
# Author: Miriam Asare-Baiden
import random
# List to store the number of attempts for each game
attempts_list = []
# Function to display the current high score
def show_score():
if not attempts_list:
print("There is currently no high score, it's yours for the taking!")
else:
print(f'The current high score is {min(attempts_list)} attempts')
# Function to start the game
def start_game():
attempts = 0
rand_num = random.randint(1, 10)
print('Hello traveler! Welcome to the game of guesses!')
player_name = input('What is your name? ')
wanna_play = input(
f'Hi, {player_name}, would you like to play the guessing game? (Enter Yes/No): ')
if wanna_play.lower() != 'yes':
print("That's cool, Thanks!")
exit()
else:
show_score()
# Main game loop
while wanna_play.lower() == 'yes':
try:
guess = int(input('Pick a number between 1 and 10: '))
if guess < 1 or guess > 10:
raise ValueError('Please guess a number within the given range')
attempts += 1
attempts_list.append(attempts)
if guess == rand_num:
print('Nice! You got it!')
print(f'It took you {attempts} attempts')
wanna_play = input('Would you like to play again? (Enter Yes/No): ')
if wanna_play.lower() != 'yes':
print("That's cool, have a good one!")
break
else:
attempts = 0
rand_num = random.randint(1, 10)
show_score()
continue
else:
if guess > rand_num:
print("It's lower")
elif guess < rand_num:
print("It's higher")
except ValueError as err:
print('Oh no!, that is not a valid value. Try again...')
print(err)
if __name__ == '__main__':
start_game()