Skip to content

Instantly share code, notes, and snippets.

@ettore26
Last active March 7, 2018 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ettore26/e45402110d4bfc97147e84be8ecb9dd1 to your computer and use it in GitHub Desktop.
Save ettore26/e45402110d4bfc97147e84be8ecb9dd1 to your computer and use it in GitHub Desktop.

The Typical 'Guess The Number' app in python.

It's gotten a proximity hint system that tells you if you're close to guess the number.

#!/usr/bin/python
import random
def getMaxNumber():
i = 0
while(True):
try:
maxNumber = int(input('Up to what number would you like to guess? '))
break
except ValueError:
if i == 2:
maxNumber = random.randint(10, 100)
print('We\'ll pick a range for you.')
break
i += 1
print('You\'ll guess from 0 to {}.'.format(maxNumber))
return maxNumber
def engine(minNumber, maxNumber, randomCloseness, guess):
while(True):
try:
number = int(input('Type a number: '))
if number < minNumber or number > maxNumber:
print('This number is out of bound.')
elif number == guess:
print('You guessed it!! Congratulations!!')
break
elif number <= (guess + randomCloseness) and number >= (guess - randomCloseness):
print('You\'r near!')
else:
print('You haven\'t guess. Keep trying.')
except ValueError:
print('This is not a number.')
def main():
try:
minNumber = 0
maxNumber = getMaxNumber()
guess = random.randint(minNumber, maxNumber)
randomCloseness = random.randint(1, 20)
engine(minNumber, maxNumber, randomCloseness, guess)
except (EOFError, KeyboardInterrupt):
print('\nBye! :(')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment