|
| 1 | +""" Number Guessing Game |
| 2 | +
|
| 3 | +---------------------------------------- |
| 4 | +
|
| 5 | +""" |
| 6 | + |
| 7 | +import random |
| 8 | + |
| 9 | +attempts_list = [] |
| 10 | + |
| 11 | +def show_score(): |
| 12 | + |
| 13 | +if len(attempts_list) <= 0: |
| 14 | + |
| 15 | +print("There is currently no high score, it's yours for the taking!") |
| 16 | + |
| 17 | +else: |
| 18 | + |
| 19 | +print("The current high score is {} attempts".format(min(attempts_list))) |
| 20 | + |
| 21 | +def start_game(): |
| 22 | + |
| 23 | +random_number = int(random.randint(1, 10)) |
| 24 | + |
| 25 | +print("Hello traveler! Welcome to the game of guesses!") |
| 26 | + |
| 27 | +player_name = input("What is your name? ") |
| 28 | + |
| 29 | +wanna_play = input("Hi, {}, would you like to play the guessing game? (Enter Yes/No) ".format(player_name)) |
| 30 | + |
| 31 | +# Where the show_score function USED to be |
| 32 | + |
| 33 | +attempts = 0 |
| 34 | + |
| 35 | +show_score() |
| 36 | + |
| 37 | +while wanna_play.lower() == "yes": |
| 38 | + |
| 39 | +try: |
| 40 | + |
| 41 | +guess = input("Pick a number between 1 and 10 ") |
| 42 | + |
| 43 | +if int(guess) < 1 or int(guess) > 10: |
| 44 | + |
| 45 | +raise ValueError("Please guess a number within the given range") |
| 46 | + |
| 47 | +if int(guess) == random_number: |
| 48 | + |
| 49 | +print("Nice! You got it!") |
| 50 | + |
| 51 | +attempts += 1 |
| 52 | + |
| 53 | +attempts_list.append(attempts) |
| 54 | + |
| 55 | +print("It took you {} attempts".format(attempts)) |
| 56 | + |
| 57 | +play_again = input("Would you like to play again? (Enter Yes/No) ") |
| 58 | + |
| 59 | +attempts = 0 |
| 60 | + |
| 61 | +show_score() |
| 62 | + |
| 63 | +random_number = int(random.randint(1, 10)) |
| 64 | + |
| 65 | +if play_again.lower() == "no": |
| 66 | + |
| 67 | +print("That's cool, have a good one!") |
| 68 | + |
| 69 | +break |
| 70 | + |
| 71 | +elif int(guess) > random_number: |
| 72 | + |
| 73 | +print("It's lower") |
| 74 | + |
| 75 | +attempts += 1 |
| 76 | + |
| 77 | +elif int(guess) < random_number: |
| 78 | + |
| 79 | +print("It's higher") |
| 80 | + |
| 81 | +attempts += 1 |
| 82 | + |
| 83 | +except ValueError as err: |
| 84 | + |
| 85 | +print("Oh no!, that is not a valid value. Try again...") |
| 86 | + |
| 87 | +print("({})".format(err)) |
| 88 | + |
| 89 | +else: |
| 90 | + |
| 91 | +print("That's cool, have a good one!") |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + |
| 95 | +start_game() |
0 commit comments