Skip to content

Instantly share code, notes, and snippets.

@jonofoz
Last active February 8, 2019 15:28
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 jonofoz/33a393e21859ec1e89d454b4e799d5f0 to your computer and use it in GitHub Desktop.
Save jonofoz/33a393e21859ec1e89d454b4e799d5f0 to your computer and use it in GitHub Desktop.
A super simple "guess my number" style game in Python.
#While still playing,
#The computer generates a number.
#The user guesses the number.
#While that number isn't correct,
#Find out if the user's guess is correct this time.
#If so,
#They win, ask them if they want to play again
#If they do,
#Go back to line 2.
#If they don't,
#They're done, the game ends
#If not,
#Make them re-guess.
#Go back to line 4.
import random as rand
def guessing_game():
playing = True
while playing:
magic_number = rand.randint(0, 25)
guess_number = int(input("Enter your guess (0 - 25): "))
# To start, the user has not guessed the correct_guess.
correct_guess = False
while not correct_guess:
if guess_number == magic_number:
print("\n\nYou win! The number was {}.\n\n".format(magic_number))
correct_guess = True
# valid_answer prevents the user from entering anything but 'y' or 'n'.
# That is, it will keep asking until the player enters a 'y' or 'n'.
valid_answer = False
while not valid_answer:
play_again = input("Play again? (y) or (n): ")
if play_again == 'n':
print("Bye!")
valid_answer = True
playing = False
elif play_again == 'y':
print("Okay!")
valid_answer = True
else:
valid_answer = False
elif guess_number < magic_number:
correct_guess = False
guess_number = int(input("Nope, the number is HIGHER.\nguess again: (0 - 25): "))
else:
correct_guess = False
guess_number = int(input("Nope, the number is LOWER.\nguess again: (0 - 25): "))
def main():
guessing_game()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment