Skip to content

Instantly share code, notes, and snippets.

@mateusmlo
Last active May 16, 2019 14:11
Show Gist options
  • Select an option

  • Save mateusmlo/9a3fef69ec20d3c7ff6b1728f9515d68 to your computer and use it in GitHub Desktop.

Select an option

Save mateusmlo/9a3fef69ec20d3c7ff6b1728f9515d68 to your computer and use it in GitHub Desktop.
Simple Python Number Guessing Game
#! /usr/bin/env python3.7
# -*- coding: UTF-8 -*-
"""
Python Number Guessing Game (with some bullshit)
"""
from random import randint
def setDifficulty():
while True:
maxValue = int(input("Set the game's difficulty: "))
if maxValue == 0:
print("Zero fun? Is this what you want?\n")
elif (maxValue % 10 != 0 or maxValue >= 501):
print("Remember rule 3! Please enter a valid number.\n")
else:
return maxValue
def main():
print("\nWelcome to the Number Guessing game!\n")
print("--RULES--\n")
print("[1] I will generate a random number between 1 and the one you input.")
print("[2] You have 5 chances to guess the number. I will give hints if you're too cold or getting hot!")
print("[3] For the difficulty, you may only input numbers multiples of 10 up to 500.")
print("[4] If you run out of chances, you d-")
print("[5] No players may be harmed throughout the game.\n")
print("You are about to set the game's difficulty.")
print("The higher the range, the harder to win!\n")
min = 1
difficulty = setDifficulty()
victoryNumber = randint(min, difficulty)
print("Alright, time to test your luck!\n")
print("I picked a number between {0} and {1}...\n".format(min, difficulty))
chances = 0
while chances < 5:
while True:
guess = int(input("What's your guess? "))
if guess == 0:
print("...are you even trying?!\n")
elif guess >= (difficulty +1):
print("Your guess can't be bigger than the range of possible numbers (1 to {0})\n".format(difficulty))
else:
break
chances = chances + 1
if guess > victoryNumber:
print("Maybe you should try a smaller number...\nYou used {0} guesses out of 5!\n".format(chances))
if guess < victoryNumber:
print("Maybe you should try a bigger number...\nYou used {0} guesses out of 5!\n".format(chances))
if guess == victoryNumber:
break
if guess != victoryNumber:
print("Nu-uh. You're out of chances! The correct number was {0}".format(victoryNumber))
if guess == victoryNumber:
print("You won! That's some good beginner's luck you got there!\n")
gameRestart = str(input("Wanna play again? Press [Y] for yes, or any other key to end: "))
if gameRestart == 'Y':
main()
if __name__ == "__main__":
main()
@mateusmlo
Copy link
Author

mateusmlo commented May 16, 2019

If you wanna test it, just run it locally with Python, or copypaste the code into a REPL website like Repl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment