Skip to content

Instantly share code, notes, and snippets.

@ramalho
Last active December 22, 2015 20:49
Show Gist options
  • Select an option

  • Save ramalho/6528571 to your computer and use it in GitHub Desktop.

Select an option

Save ramalho/6528571 to your computer and use it in GitHub Desktop.
Simple number guessing game
#!/usr/bin/env python3
# guess.py: a simple number guessing game
from random import randint
print('Hello, please type your name:')
name = input()
print('Hi, ', name, '!', sep='')
print("I will select a number from 1 to what number? You can choose, dear user.")
upperbound = int(input())
print("Ok. Let's see if you guess it.")
number = randint(1, upperbound)
tries = 0
while tries < 10:
tries = tries + 1
print('Take a guess:')
guess = int(input())
if guess == number:
if tries == 1:
print('You guessed it, ' + name + '! It took you ' + str(tries) + " try.")
else:
print('You guessed it, ' + name + '! It took you ' + str(tries) + " tries.")
break
elif guess > number:
print('Too high.')
else:
print('Too low.')
else:
print('The correct number was:', number)
print('Game over')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment