Tichomir Dunlop
Published © CC BY-NC-ND

RGB LED: Math Quiz

Test your multiplication tables with a python program. Correct, the LED flashes 1 sec. Incorrect, the LED flashes 0.05 sec.

IntermediateShowcase (no instructions)30 minutes740

Things used in this project

Hardware components

Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
LED (generic)
LED (generic)
The reason I did red and blue instead of red and green is because in my previous project (RGB LED: Change Color Through Python.) the green part of my LED broke.
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×3
Raspberry Pi 1 Model B+
Raspberry Pi 1 Model B+
×1
Speakers (generic)
×1

Software apps and online services

Putty
WinSCP

Code

Python Test Code OUTDATED

Python
OUTDATED Test script for this project. Note: Press Ctrl+C to clean up. Please run in Python 3.
#Note: This is a test script and will not work with LED.
import random
print("Welcome to the multiplication game!")
print("Answer correctly, the LED goes blue.")
print("Answer incorrectly, the LED goes red.")
print(" ")
x = random.randint(1, 12)
y = random.randint(1, 12)
z = str(x*y)
x = str(x)
y = str(y)
print(x + " x " + y + " = ?")
UserInput = input()
#In final script, run in Python 2 and use raw_input() instaed of input()
UserInput = str(UserInput)
#This way, both z and UserInput are strings. Otherwise, the program says 6*7!=42
if UserInput == z:
    print("CORRECT!")
else:
    print("WRONG, BOZO!")

Python Code OUTDATED

Python
Critical script for this project, will not work without. Note: Press Ctrl+C to clean up, raw_input() will NOT work in Python 3. Please run in Python 2.
import RPi.GPIO as GPIO
import random
red_pin = 18
blue_pin = 24
GPIO.setup(red_pin, GPIO.OUT)
GPIO.setup(blue_pin, GPIO.OUT)
GPIO.output(red_pin, False)
GPIO.output(blue_pin, False)
try:         
    while True:
        print("Welcome to the multiplication game!")
        print("Answer correctly, the LED goes blue.")
        print("Answer incorrectly, the LED goes red.")
        print()
        x = random.randint(1, 12)
        y = random.randint(1, 12)
        z = str(x*y)
        x = str(x)
        y = str(y)
        print(x + " x " + y + " = ?")
        UserInput = raw_input()
        #raw_input does not work in Python 3, please run in Python 2
        if UserInput == z:
            print("CORRECT!")
            GPIO.output(red_pin, False)
            GPIO.output(blue_pin, True)
        else:
            print("WRONG, BOZO!")
            GPIO.output(red_pin, True)
            GPIO.output(blue_pin, False)
finally:
    print("Cleaning up.")
    GPIO.cleanup()

Python Test Code FINAL

Python
This is the final test code for the project, created 7/13-14/2016.
#import RPi.GPIO as GPIO
import random
import time
#GPIO.setwarnings(False)
#GPIO.setmode(GPIO.BCM)
#red_pin = 18
#GPIO.setup(red_pin, GPIO.OUT)
insult_list = ["What an idiot! You don't know your multiplication table!", "Have you heard? YOU'RE STUPID!", "Back to elementary school..."]
compliment_list = ["Know-it-all... I'll show you!", "Lucky guess.", "Beginners' luck."]
print("Welcome to the multiplication quiz!")
print("How many questions do you want?")
questions = int(input())
strQuestions = str(questions)
askedQuestions = 0
correctQuestions = 0
while askedQuestions < questions:
    x = random.randint(1, 12)
    y = random.randint(1, 12)
    z = x*y
    print("What is " + str(x) + " x " + str(y) + "?")
    UserInput = input()
    if UserInput == x*y:
        print(random.choice(compliment_list))
        #GPIO.output(red_pin, True)
        #time.sleep(1)
        #GPIO.output(red_pin, False)
        correctQuestions += 1
    else:
        print(random.choice(insult_list))
        #GPIO.output(red_pin, True)
        #time.sleep(0.05)
        #GPIO.output(red_pin, False)
    askedQuestions += 1
print("Fun and games are over.")
score = float(correctQuestions)/float(questions)*100 
print("Your score is " + str(score) + "%.")
if float(score) > 89:
    letterScore = "A"
    if questions == 1:
    	print("Coward! You don't deserve an A! YOU ONLY HAD ONE QUESTION MORON.")
    else:
        print("People these days... smarter than they used to be!")
elif score > 79 and float(score) < 90:
    letterScore = "B"
    print("Pretty good... BUT NOT PERFECT! Nobody wants B students.")
elif score > 69 and float(score) < 80:
    letterScore = "C"
    print("You have a C?!? Practice more, weakling -- practice more.")
elif score > 59 and float(score) < 70:
    letterScore = "D"
    if correctQuestions == 1:
    	print("The on you got right was a lucky guess.")
    else:
        print("The few you got right were just beginners' luck.")
elif float(score) < 60:
    letterScore = "F"
    if questions == 1:
    	print("You had ONE question.. AND YOU GOT IT WRONG!!! What an IDIOT.")
    print("Get out of my sight, vermin!")
print("Your grade is an " + letterScore + ".")
print("Asked Quesions: " + str(questions) + " Correct Questions: " + str(correctQuestions))

Python Code FINAL

Python
This is the final code for the project, created 7/13-14/2016.
import RPi.GPIO as GPIO
import random
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
red_pin = 18
GPIO.setup(red_pin, GPIO.OUT)
insult_list = ["What an idiot! You don't know your multiplication table!", "Have you heard? YOU'RE STUPID!", "Back to elementary school..."]
compliment_list = ["Know-it-all... I'll show you!", "Lucky guess.", "Beginners' luck."]
print("Welcome to the multiplication quiz!")
print("How many questions do you want?")
questions = int(input())
strQuestions = str(questions)
askedQuestions = 0
correctQuestions = 0
while askedQuestions < questions:
    x = random.randint(1, 12)
    y = random.randint(1, 12)
    z = x*y
    print("What is " + str(x) + " x " + str(y) + "?")
    UserInput = input()
    if UserInput == x*y:
        print(random.choice(compliment_list))
        GPIO.output(red_pin, True)
        time.sleep(1)
        GPIO.output(red_pin, False)
        correctQuestions += 1
    else:
        print(random.choice(insult_list))
        GPIO.output(red_pin, True)
        time.sleep(0.05)
        GPIO.output(red_pin, False)
    askedQuestions += 1
print("Fun and games are over.")
score = float(correctQuestions)/float(questions)*100 
print("Your score is " + str(score) + "%.")
if float(score) > 89:
    letterScore = "A"
    if questions == 1:
    	print("Coward! You don't deserve an A! YOU ONLY HAD ONE QUESTION MORON.")
    else:
        print("People these days... smarter than they used to be!")
elif score > 79 and float(score) < 90:
    letterScore = "B"
    print("Pretty good... BUT NOT PERFECT! Nobody wants B students.")
elif score > 69 and float(score) < 80:
    letterScore = "C"
    print("You have a C?!? Practice more, weakling -- practice more.")
elif score > 59 and float(score) < 70:
    letterScore = "D"
    if correctQuestions == 1:
    	print("The on you got right was a lucky guess.")
    else:
        print("The few you got right were just beginners' luck.")
elif float(score) < 60:
    letterScore = "F"
    if questions == 1:
    	print("You had ONE question.. AND YOU GOT IT WRONG!!! What an IDIOT.")
    print("Get out of my sight, vermin!")
print("Your grade is an " + letterScore + ".")
print("Asked Quesions: " + str(questions) + " Correct Questions: " + str(correctQuestions))

COMPLETE PROJECT

Python
Connect -speakers -LED. Completed July 17
import RPi.GPIO as GPIO
import random
import time
print("Would you like audio?")
audio = "default"
while audio != "yes" and audio != "no":
	audio = raw_input() 
	if audio == "yes":
		print("Audio on.")
	elif audio == "no":
		print("Audio off.")
	else:
		print("Sorry, I do not understand.")
		print("Only 'yes' and 'no' are valid answers.")
def playSound_Full(sound):
	if audio == "yes":
		pygame.mixer.music.load(sound)
		pygame.mixer.music.play()
		while pygame.mixer.music.get_busy() == True:
			continue
if audio == "yes":
	print("Loading pygame...")
	import pygame
	time.sleep(0.5)
	print("100% Complete.")
	time.sleep(1)
	print("Initializing audio...")
	pygame.mixer.init()
	time.sleep(0.5)
	print("100% Complete.")
	time.sleep(1)
	print("Testing audio...")
	pygame.mixer.music.set_volume(1.0)
	pygame.mixer.music.load("/home/pi/Desktop/Python Games/beep.mp3")
	pygame.mixer.music.play()
	time.sleep(0.5)
	print("100% Complete")
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
red_pin = 18
GPIO.setup(red_pin, GPIO.OUT)
insult_list = ["What an idiot! You don't know your multiplication table!", "Have you heard? YOU'RE STUPID!", "Back to elementary school..."]
compliment_list = ["Know-it-all... I'll show you!", "Lucky guess.", "Beginners' luck."]
print("Welcome to the multiplication quiz!")
print("How many questions do you want?")
questions = input()
askedQuestions = 0
correctQuestions = 0
while askedQuestions < questions:
    x = random.randint(1, 12)
    y = random.randint(1, 12)
    print("What is " + str(x) + " x " + str(y) + "?")
    UserInput = raw_input()
    if int(UserInput) == x*y: #correct
        print(random.choice(compliment_list)) #Compliment user
    	GPIO.output(red_pin, True)
        time.sleep(1)
        GPIO.output(red_pin, False)
        correctQuestions += 1
        playSound_Full("/home/pi/Desktop/Python Games/bomb.mp3")
    else: #incorrect
        print(random.choice(insult_list)) #Insult user
	time.sleep(1)
        print("The correct answer is " + str(x*y) + ".")
	GPIO.output(red_pin, True)
        time.sleep(0.05)
        GPIO.output(red_pin, False)
        playSound_Full("/home/pi/Desktop/Python Games/trombone.mp3")
    askedQuestions += 1
time.sleep(1)
print("Fun and games are over.")
score = float(correctQuestions)/questions*100 
print("Your score is " + str(round(score, 2)) + "%.")
if round(score, 2) != score:
    print("Would you like to see your score as an exact decimal?")
    print("It has been rounded to two decimal places.")
    UserInput = "default"
    while UserInput != "yes" and UserInput != "no":
        UserInput = raw_input()
    	if UserInput == "yes":
    		print("Your score is exactly " + str(score) + "%.")
   	elif UserInput == "no":
    		print("Okay then. If you change your mind,")
    		print("just divide how many correct questions you have")
        	print("by how many total questions.")
    	else:
    		print("Sorry, I do not understand. Only 'yes' and 'no' are valid answers.")
if float(score) > 89:
    letterScore = "A"
    if questions == 1:
    	print("Coward! You don't deserve an A! YOU ONLY HAD ONE QUESTION MORON.")
    else:
        print("People these days... not as dumb as they used to be! What a shame.")
elif score > 79 and float(score) < 90:
    letterScore = "B"
    print("Pretty good... BUT NOT PERFECT! Nobody wants B students.")
elif score > 69 and float(score) < 80:
    letterScore = "C"
    print("You have a C?!? Practice more, weakling -- practice more.")
elif score > 59 and float(score) < 70:
    letterScore = "D"
    if correctQuestions == 1:
    	print("The one you got right was a lucky guess.")
    else:
        print("The few you got right were just beginners' luck.")
elif float(score) < 60:
    letterScore = "F"
    if questions == 1:
    	print("You had ONE question.. AND YOU GOT IT WRONG!!! What an IDIOT.")
    print("Get out of my sight, vermin!")
print("Your grade is an " + letterScore + ".")
print("Asked Quesions: " + str(questions) + " Correct Questions: " + str(correctQuestions))
playSound_Full("/home/pi/Desktop/Python Games/buzzer.mp3")
playSound_Full("/home/pi/Desktop/Python Games/botvoice.mp3")

Credits

Tichomir Dunlop
5 projects • 2 followers
Hello! I am an beginner on the Raspberry Pi and have a model 3 and B+. I know some Python Programming Language as well.
Contact

Comments

Please log in or sign up to comment.