Skip to content

Instantly share code, notes, and snippets.

@Ceasar Ceasar/game.py
Created Apr 7, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.