ETechSavvies

Number Guessing Game in PYTHON

Apr 26th, 2021
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. """
  2.    Written by: ETechSavvies [https://t.me/ETechSavvies] Apr 10,2021
  3.    Description: Number Guessing Game
  4.    Copyright 2021 @ETechSavvies
  5. """
  6.  
  7. import random
  8.  
  9. # Generates a random number between 0 and 50
  10. number = random.randint(0, 50)
  11.  
  12. command = "yes"
  13. number_of_guess = 0
  14.  
  15. name_of_player = input("Hello. What's your name? ")
  16. print(f"Nice to meet you {name_of_player.title()}. I'm thinking of a number between 0 and 50, guess what it is: ")
  17.  
  18. # Keeps running till the user enters no and attempts to guess morethan 5 times
  19. while command != "no" and number_of_guess < 5:
  20.     guess = int(input())
  21.  
  22.     # Increment number_of_guess by 1 everytime the user guesses a number
  23.     number_of_guess += 1
  24.  
  25.     # Handles the error caused if the user enters a number,
  26.     # which is out of the range in the randint() function
  27.     if guess < 0 or guess > 50:
  28.         print("Enter a valid guess")
  29.  
  30.     elif guess > number:
  31.         print("Your guess is too high! Guess again: ")
  32.  
  33.     elif guess < number:
  34.         print("Your guess is too low! Guess again: ")
  35.  
  36.     else:
  37.  
  38.         # Check if the user wants to play again or not once he/she finds the number
  39.         command = input("Congrats! You found the number. Want to continue the game? yes/no: ").lower()
  40.  
  41.         if command == 'yes':
  42.             # We set this back to 0 because it's a new game now
  43.             number_of_guess = 0
  44.  
  45.             print("I'm thinking of a number between 0 and 50, guess what it is: ")
  46.  
  47.         else:
  48.             print(f"Thanks for playing the game! {name_of_player.title()}")
  49.  
  50. # After the 5th attempt, end the game
  51. if number_of_guess == 5:
  52.     print(f"Too many trials {name_of_player.title()}. The number is {number}. Good luck next time!")
Advertisement
Add Comment
Please, Sign In to add comment