Skip to content

Instantly share code, notes, and snippets.

@pzp1997
Created May 7, 2014 01:10
Show Gist options
  • Save pzp1997/52bc41ac4643cf779cb1 to your computer and use it in GitHub Desktop.
Save pzp1997/52bc41ac4643cf779cb1 to your computer and use it in GitHub Desktop.
from random import randint, choice
from time import sleep
RPi = True
try:
import RPi.GPIO as GPIO
except ImportError:
RPi = False
if RPi:
greenLED = 18
redLED = 16
GPIO.setup(greenLED, GPIO.OUT, initial=False)
GPIO.setup(redLED, GPIO.OUT, initial=False)
operations = ["x", "/", "+", "-"]
while True:
num = [randint(0, 12), randint(0, 12)]
num.sort()
operation = choice(operations)
if operation == "x":
correct = num[1]*num[0]
elif operation == "/":
while num[0] == 0 or num[1] == 0 or (num[1]/num[0])%1 != 0:
num = [randint(1, 12), randint(1, 12)]
num.sort()
correct = str(num[1]/num[0])[:-2]
elif operation == "+":
correct = num[1] + num[0]
else:
correct = num[1] - num[0]
correct = str(correct)
answer = input(str(num[1]) + " " + operation + " " + str(num[0]) + " = ")
if answer == correct:
print("Correct!")
if RPi:
GPIO.output(greenLED, True)
sleep(1)
GPIO.output(greenLED, False)
else:
if answer == "q":
raise SystemExit
print("Sorry, the correct answer was " + correct + " :(")
if RPi:
GPIO.output(redLED, True)
sleep(1)
GPIO.output(redLED, False)
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment