|
1 | 1 | import random
|
2 | 2 |
|
3 |
| -""" |
4 |
| - Funtion start() create the magic number, number of attemps, and ask user for input |
5 |
| -""" |
6 | 3 | def start():
|
7 |
| - global magic_number |
8 |
| - magic_number = random.randint(1, 100) |
9 |
| - |
10 |
| - global attempts |
| 4 | + """ |
| 5 | + Creates the magic number, number of attempts, and asks the user for input. |
| 6 | + """ |
| 7 | + global magic_number, attempts |
| 8 | + magic_number = choose_difficulty() |
11 | 9 | attempts = 0
|
12 |
| - |
13 | 10 | print('---------------------------')
|
14 |
| - print('Guess a number between 1 and 100') |
| 11 | + print('Guess the magic number!') |
| 12 | + print('Try to guess the number in as few attempts as possible.') |
| 13 | + |
| 14 | +def choose_difficulty(): |
| 15 | + """ |
| 16 | + Allows the user to select the difficulty level and returns the corresponding range for the magic number. |
| 17 | + """ |
| 18 | + print("Choose difficulty level:") |
| 19 | + print("1. Easy (1-50)") |
| 20 | + print("2. Medium (1-100)") |
| 21 | + print("3. Hard (1-200)") |
| 22 | + |
| 23 | + while True: |
| 24 | + level = input("Enter 1, 2, or 3: ") |
| 25 | + if level == '1': |
| 26 | + return random.randint(1, 50) |
| 27 | + elif level == '2': |
| 28 | + return random.randint(1, 100) |
| 29 | + elif level == '3': |
| 30 | + return random.randint(1, 200) |
| 31 | + else: |
| 32 | + print("Invalid choice. Please enter 1, 2, or 3.") |
15 | 33 |
|
16 |
| -# Function that checks if player won, if it won, returns True |
17 | 34 | def check_win(player_guess):
|
| 35 | + """ |
| 36 | + Checks if the player’s guess is correct, too high, or too low. |
| 37 | + """ |
18 | 38 | if player_guess > magic_number:
|
19 |
| - print('Too big...') |
| 39 | + print('Too high! Try a smaller number.') |
20 | 40 | elif player_guess < magic_number:
|
21 |
| - print('Too small') |
| 41 | + print('Too low! Try a larger number.') |
22 | 42 | elif player_guess == magic_number:
|
23 | 43 | return True
|
24 | 44 |
|
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() |
| 45 | +def play_game(): |
| 46 | + """ |
| 47 | + Main game loop that starts the game, processes user input, and checks for win conditions. |
| 48 | + """ |
| 49 | + global attempts |
| 50 | + start() |
| 51 | + |
| 52 | + while True: |
| 53 | + try: |
| 54 | + guess = int(input('Enter your guess: ')) |
| 55 | + if guess < 1 or guess > 200: |
| 56 | + print("Please enter a number between 1 and 200.") |
| 57 | + continue |
| 58 | + except ValueError: |
| 59 | + print("Invalid input. Please enter a valid number.") |
| 60 | + continue |
| 61 | + |
| 62 | + attempts += 1 |
| 63 | + |
| 64 | + if check_win(guess): |
| 65 | + print(f'Congratulations! You guessed the number in {attempts} attempts.') |
| 66 | + keep_playing = input('Would you like to play again? (y/n): ') |
| 67 | + if keep_playing.lower() == 'y': |
| 68 | + play_game() |
| 69 | + elif keep_playing.lower() == 'n': |
| 70 | + print("Thanks for playing! Have a great day!") |
| 71 | + break |
| 72 | + else: |
| 73 | + print("Invalid input. Exiting the game.") |
| 74 | + break |
| 75 | + |
| 76 | +play_game() |
0 commit comments