Advertisement
Talithacelin

Guess the Number

Sep 25th, 2022 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. #GUESS THE NUMBER
  2.  
  3. import random
  4. import sys
  5. attempts_list = []
  6.  
  7. def show_score():
  8.     if len(attempts_list) <= 0:
  9.         print("No score yet! Play the game to get a score!")
  10.     else :
  11.         print(f"Last score achieved with {attempts_list} attempts!")
  12.         print(f"Beat your highscore of {min(attempts_list)} attempts!")
  13.  
  14.  
  15. def start_game():
  16.     random_num = int(random.randint(1,10))
  17.     print("-------------------------")
  18.     print("Guess the Number Game!")
  19.     print("-------------------------")
  20.     name = input("Please enter your name: ")
  21.     ready = input("Are you ready " + name + " ? ")
  22.     attempts = 0
  23.     show_score()
  24.  
  25.  
  26.     while ready == "Yes" or ready == "yes":
  27.         try:
  28.             print()
  29.             guess = int(input("Guess a number 1-10! "))
  30.  
  31.             if int(guess) < 0 or int(guess) > 10:
  32.                 raise ValueError
  33.            
  34.             if int(guess) == random_num:
  35.                 print(f"GREAT {name}! You've guessed it!")
  36.                 attempts += 1
  37.                 attempts_list.append(attempts)
  38.                 print(f"With a total of {attempts} attempts!")
  39.  
  40.                 play_again = input("Wanna play again? ")
  41.                 attempts = 0
  42.                 show_score()
  43.                 random_num = int(random.randint(1, 10))
  44.  
  45.                 if play_again == "no" or play_again == "No":
  46.                     print("Cool! Do try again next time!")
  47.                     sys.exit()
  48.            
  49.             elif int(guess) < random_num:
  50.                 print("Go Higher!")
  51.                 attempts += 1
  52.            
  53.             elif int(guess) > random_num:
  54.                 print("Go Lower!")
  55.                 attempts += 1
  56.  
  57.         except ValueError:
  58.             print("Not a valid answer..!")
  59.             print("Please enter a number from 1-10 & NOT a letter.")
  60.            
  61.     else:
  62.         print()
  63.         print("Okeyy bye!")
  64.  
  65. start_game()
  66.  
  67. #Talice ~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement