Created
May 7, 2014 01:10
-
-
Save pzp1997/52bc41ac4643cf779cb1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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