Skip to content

Instantly share code, notes, and snippets.

@Ceasar
Created April 7, 2019 18:42
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 Ceasar/f7bf1b2e9691e58b1cb8ef12c0385e58 to your computer and use it in GitHub Desktop.
Save Ceasar/f7bf1b2e9691e58b1cb8ef12c0385e58 to your computer and use it in GitHub Desktop.
import random
def main():
while True:
a, b = random.randint(1, 10), random.randint(1, 10)
expr = "{} + {}".format(a, b)
print "What is {}?".format(expr)
try:
given_answer = int(raw_input('> '))
except ValueError:
print 'Wrong. Not a number.'
else:
right_answer = eval(expr)
if given_answer == right_answer:
print 'Correct!'
break
else:
print 'Wrong. The right answer was {}.'.format(right_answer)
print 'Game over.'
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment