Assignment 2
- ncampanelli0
- Sep 10, 2019
- 1 min read
I wrote a magic eight ball program in python using the micro-python library. The program works by shaking the device to display a yes, no or maybe answer which is denoted by the microbit displaying shapes on the led display and lighting up leds that were wired on the breadboard. Pressing either button will reset the program, in short a shutdown button. The random number to determine yes, no or maybe is achieved with use of importing the random library into my project (random.randint).
Problems I had with making this program mostly boiled down to trying to attach the shake check to a variable (did not work, in the end I replaced the variable name in one of the functions directly with the built-in function designed to check for shaking) and using else if instead of elif (force of habit).
code:
from microbit import *
from random import *
"""
pin 0 = no
pin 1 = yes
pin 0 + 1 = maybe
"""
#global vars
#shakeCheck = acceleromter.is_gesture(shake)
answer = -1
picYes = Image("00000:00009:00090:90900:09000:")
picNo = Image("90009:09090:00900:09090:90009:")
picMaybe = Image("00000:00000:99999:00000:00000:")
def getAnswer():
while accelromter.is_gesture(shake):
answer = random.randrange(0,2)
def eBall():
if answer == 0:
pin0.write_digital(1)
pin1.write_digital(0)
picNo
elif answer == 1:
pin0.write_digital(0)
pin1.write_digital(1)
picYes
elif answer == 2:
pin0.write_digital(1)
pin1.write_digital(1)
picMaybe
else:
pin0.write_digital(0)
pin1.write_digital(0)
def resetAnswer():
if button_a.is_pressed() or button_b.is_pressed():
answer = -1
def main():
while True:
getAnswer()
eBall()
resetAnswer()
if __name__ == "__main__":
main()
Comments