|
1 | 1 | from random import randrange |
| 2 | + |
2 | 3 | def main(): |
3 | | - randomized_no = randomize_no() |
| 4 | + print("Welcome to Number Guessing Game!") |
| 5 | + level = 1 |
| 6 | + total_score = 0 |
| 7 | + |
4 | 8 | while True: |
5 | | - user_guessed_input = guess() |
6 | | - if int(user_guessed_input) > randomized_no: |
7 | | - print("Too large!") |
8 | | - continue |
9 | | - elif int(user_guessed_input) < randomized_no: |
10 | | - print("Too small!") |
11 | | - continue |
| 9 | + print(f"\n--- Level {level} ---") |
| 10 | + max_number = get_level_max(level) |
| 11 | + randomized_no = randrange(1, max_number + 1) |
| 12 | + attempts_left = 5 # max attempts per level |
| 13 | + |
| 14 | + while attempts_left > 0: |
| 15 | + user_guess = get_guess(level) |
| 16 | + if user_guess > randomized_no: |
| 17 | + print("Too high! Try again.") |
| 18 | + elif user_guess < randomized_no: |
| 19 | + print("Too low! Try again.") |
| 20 | + else: |
| 21 | + print(f"Correct! You've cleared Level {level}.") |
| 22 | + total_score += attempts_left * 10 # more points for fewer attempts |
| 23 | + break |
| 24 | + attempts_left -= 1 |
| 25 | + print(f"Attempts left: {attempts_left}") |
12 | 26 | else: |
13 | | - print("Just right!") |
| 27 | + print(f"Game Over! The number was {randomized_no}.") |
14 | 28 | break |
| 29 | + |
| 30 | + level += 1 |
| 31 | + print(f"Total Score: {total_score}") |
| 32 | + cont = input("Do you want to continue to the next level? (y/n): ").lower() |
| 33 | + if cont != 'y': |
| 34 | + print(f"Thanks for playing! Final Score: {total_score}") |
| 35 | + break |
| 36 | + |
15 | 37 | def is_positive_integer(n): |
16 | | - """This function takes an input and check if the user input is an integer""" |
| 38 | + try: |
| 39 | + num = int(n) |
| 40 | + return num > 0 |
| 41 | + except ValueError: |
| 42 | + return False |
| 43 | + |
| 44 | +def get_guess(level): |
17 | 45 | while True: |
18 | | - try: |
19 | | - num = int(n) |
20 | | - if int(num) < 1: |
21 | | - return False |
22 | | - except ValueError: |
23 | | - return False |
| 46 | + guess_input = input(f"Level {level} Guess: ") |
| 47 | + if is_positive_integer(guess_input): |
| 48 | + return int(guess_input) |
24 | 49 | else: |
25 | | - return True |
26 | | -def get_user_input(): |
27 | | - """Prompt the user for an input and check if it's a positive integer""" |
28 | | - while True: |
29 | | - user_input = input("Level 1: ") |
30 | | - if (is_positive_integer(user_input)): |
31 | | - return int(user_input) |
32 | | -def guess(): |
33 | | - "Prompt the user for an input guess, and check if it's an integer" |
34 | | - while True: |
35 | | - user_guess = input("Guess: ") |
36 | | - if is_positive_integer(user_guess): |
37 | | - return user_guess |
38 | | -def randomize_no(): |
39 | | - """Randomize number""" |
40 | | - user_inputted_number = get_user_input() |
41 | | - if int(user_inputted_number) > 1: |
42 | | - random_number = randrange(1, user_inputted_number) |
43 | | - return random_number |
44 | | - else: |
45 | | - return int(user_inputted_number) |
| 50 | + print("Enter a valid positive integer!") |
| 51 | + |
| 52 | +def get_level_max(level): |
| 53 | + """Increase range as levels go up""" |
| 54 | + return 10 + (level - 1) * 5 |
| 55 | + |
46 | 56 | if __name__ == "__main__": |
47 | 57 | main() |
0 commit comments