Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Written by: ETechSavvies [https://t.me/ETechSavvies] Apr 10,2021
- Description: Number Guessing Game
- Copyright 2021 @ETechSavvies
- """
- import random
- # Generates a random number between 0 and 50
- number = random.randint(0, 50)
- command = "yes"
- number_of_guess = 0
- name_of_player = input("Hello. What's your name? ")
- print(f"Nice to meet you {name_of_player.title()}. I'm thinking of a number between 0 and 50, guess what it is: ")
- # Keeps running till the user enters no and attempts to guess morethan 5 times
- while command != "no" and number_of_guess < 5:
- guess = int(input())
- # Increment number_of_guess by 1 everytime the user guesses a number
- number_of_guess += 1
- # Handles the error caused if the user enters a number,
- # which is out of the range in the randint() function
- if guess < 0 or guess > 50:
- print("Enter a valid guess")
- elif guess > number:
- print("Your guess is too high! Guess again: ")
- elif guess < number:
- print("Your guess is too low! Guess again: ")
- else:
- # Check if the user wants to play again or not once he/she finds the number
- command = input("Congrats! You found the number. Want to continue the game? yes/no: ").lower()
- if command == 'yes':
- # We set this back to 0 because it's a new game now
- number_of_guess = 0
- print("I'm thinking of a number between 0 and 50, guess what it is: ")
- else:
- print(f"Thanks for playing the game! {name_of_player.title()}")
- # After the 5th attempt, end the game
- if number_of_guess == 5:
- 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