Skip to content

Instantly share code, notes, and snippets.

@LiamSwanepoel
Created May 22, 2022 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LiamSwanepoel/d89356f8fe9fe03e94bb21e7440d6fd8 to your computer and use it in GitHub Desktop.
Save LiamSwanepoel/d89356f8fe9fe03e94bb21e7440d6fd8 to your computer and use it in GitHub Desktop.
A simple guessing game where the player has to guess the randomly generated number, if the player guesses too high, the program will tell the player they guessed to high, and the same for if the players guesses too low or guesses correctly
import random
num = random.randint(1, 9)
while True:
try:
guess = int(input("\nPlease enter a guess from 1-9: "))
if 0 < guess < 10:
if guess > num:
print("\nYou guessed too high")
elif guess < num:
print("\nYou guessed too low")
elif guess == num:
print("\nYou guessed correctly")
while True:
u_input = input("\nWould you like to play again? y/n: ")
if u_input == 'n':
exit()
elif u_input == 'y':
num = random.randint(1, 9)
break
elif u_input != 'y' and u_input != 'n':
print("\nError: Please select a valid option")
elif guess < 1 or guess > 9:
print("\nError: Please enter a number from 1-9")
except ValueError:
print("\nError: Please enter a number")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment