PythonEditor
PythonEditor copied to clipboard
Having some trouble with my program
I am not sure if the problem is microbit or python itself.
It is a Morse Code translator and the error is most likely on Line 40
https://pastebin.com/HuAfMdca
Directly embedding snippet:
# Add your Python code here. E.g.
from microbit import *
#LINE 40 is the error
times = 0
a_presses = 0
b_presses = 0
section1 = 'Not Defined'
section2 = 'Not Defined'
section3 = 'Not Defined'
main = True
sub = True
loop1 = True
loop2 = True
loop3 = True
contin = True
letter = 'Error: No Letter Found'
while main:
while sub:
#First Time
display.scroll("GO1")
while loop1:
sleep(3000)
if button_a.get_presses() == 1:
a_presses = button_a.get_presses()
b_presses = button_b.get_presses()
section1 = '0'
loop1 = False;
if button_b.get_presses() == 1:
a_presses = button_a.get_presses()
b_presses = button_b.get_presses()
section1 = '1'
loop1 = False;
while loop2:
display.scroll("GO2")
sleep(3000)
#PROBLEM BELOW: which ever button (A/B) goes first, will not work for Part2
if button_b.get_presses() > b_presses and button_a.get_presses == a_presses and contin == True:
a_presses = button_a.get_presses()
b_presses = button_b.get_presses()
section2 = '1'
loop2 = False
if button_a.get_presses() > a_presses and button_b.get_presses == b_presses and contin == True:
a_presses = button_a.get_presses()
b_presses = button_b.get_presses()
section2 = '0'
loop2 = False
if button_b.get_presses() > b_presses and button_a.get_presses() > a_presses and contin == True:
section2 = '2'
loop2 = False
if section2 == '2':
if section1 == '0':
letter = 'E'
if section1 == '1':
letter = 'T'
contin = False
sub = False
while loop3:
display.scroll("GO3")
sleep(3000)
if button_a.get_presses() > a_presses and button_b.get_presses() > b_presses and contin == True:
section3 = '2'
loop3 = False
if button_a.get_presses() > a_presses and contin == True:
a_presses = button_a.get_presses()
b_presses = button_b.get_presses()
section3 = '0'
loop3 = False
if button_b.get_presses() > b_presses and contin == True:
a_presses = button_a.get_presses()
b_presses = button_b.get_presses()
section3 = '1'
loop3 = False
if section3 == '2':
if section1 == '0' and section2 == '1':
letter = 'A'
if section1 == '0' and section2 == '0':
letter = 'I'
if section1 == '1' and section2 == '0':
letter = 'N'
if section1 == '1' and section2 == '1':
letter = 'M'
contin = False
sub = False
display.show(letter)
So you suspected the problem was around Line 40:
if button_b.get_presses() > b_presses and button_a.get_presses == a_presses and contin == True:
your problem is painfully simple yet easily overlooked, Let's break this statement up:
button_b.get_presses() > b_presses
button_a.get_presses == a_presses
contin == True
At this point i'm sure you see your mistake, you've forgotten the parentesis (()) after button_a.get_presses mean that instead of comparing the result of get_presses to a_presses you are infact comparing the function itself.
So the corrected statement is:
if button_b.get_presses() > b_presses and button_a.get_presses() == a_presses and contin:
can't believe I didn't notice that! Thanks a lot!
This time there is a different error ): I added a section4 and if the input for 'section3' is 'A' then section3 will replay itself
# Add your Python code here. E.g.
from microbit import *
times = 0
a_presses = 0
b_presses = 0
section1 = 'Not Defined'
section2 = 'Not Defined'
section3 = 'Not Defined'
section4 = 'Not Defined'
main = True
sub = True
loop1 = True
loop2 = True
loop3 = True
loop4 = True
contin = True
letter = 'Error: No Letter Found'
while main:
while sub:
#First Time
display.scroll("GO1")
while loop1:
sleep(3000)
if button_a.get_presses() == 1:
a_presses = button_a.get_presses()
b_presses = button_b.get_presses()
section1 = '0'
loop1 = False;
if button_b.get_presses() == 1:
a_presses = button_a.get_presses()
b_presses = button_b.get_presses()
section1 = '1'
loop1 = False;
while loop2:
display.scroll("GO2")
sleep(3000)
if button_b.get_presses() > b_presses and button_a.get_presses() == a_presses and contin == True:
a_presses = button_a.get_presses()
b_presses = button_b.get_presses()
section2 = '1'
loop2 = False
if button_a.get_presses() > a_presses and button_b.get_presses() == b_presses and contin == True:
a_presses = button_a.get_presses()
b_presses = button_b.get_presses()
section2 = '0'
loop2 = False
if button_b.get_presses() > b_presses and button_a.get_presses() > a_presses and contin == True:
section2 = '2'
loop2 = False
if section2 == '2':
if section1 == '0':
letter = 'E'
if section1 == '1':
letter = 'T'
contin = False
sub = False
while loop3:
display.scroll("GO3")
sleep(3000)
if button_a.get_presses() > a_presses and button_b.get_presses() > b_presses and contin == True:
section3 = '2'
loop3 = False
if button_a.get_presses() > a_presses and contin == True:
a_presses = button_a.get_presses()
b_presses = button_b.get_presses()
section3 = '0'
loop3 = False
if button_b.get_presses() > b_presses and contin == True:
a_presses = button_a.get_presses()
b_presses = button_b.get_presses()
section3 = '1'
loop3 = False
if section3 == '2':
if section1 == '0' and section2 == '1':
letter = 'A'
if section1 == '0' and section2 == '0':
letter = 'I'
if section1 == '1' and section2 == '0':
letter = 'N'
if section1 == '1' and section2 == '1':
letter = 'M'
contin = False
sub = False
while loop4:
display.scroll("GO4")
sleep(3000)
if button_a.get_presses() > a_presses and button_b.get_presses() > b_presses and contin == True:
section4 = '2'
loop4 = False
if button_a.get_presses() > a_presses and contin == True:
a_presses = button_a.get_presses()
b_presses = button_b.get_presses()
section4 = '0'
loop4 = False
if button_b.get_presses() > b_presses and contin == True:
a_presses = button_a.get_presses()
b_presses = button_b.get_presses()
section4 = '1'
loop4 = False
if section4 == '2':
if section1 == '0' and section2 == '0' and section3 == '0':
letter = 'S'
if section1 == '0' and section2 == '0' and section3 == '1':
letter = 'U'
if section1 == '0' and section2 == '1' and section3 == '0':
letter = 'R'
if section1 == '0' and section2 == '1' and section3 == '1':
letter = 'W'
if section1 == '1' and section2 == '1' and section3 == '1':
letter = 'O'
if section1 == '1' and section2 == '0' and section3 == '0':
letter = 'D'
if section1 == '1' and section2 == '1' and section3 == '0':
letter = 'G'
if section1 == '0' and section2 == '1' and section3 == '0':
letter = 'R'
contin = False
sub = False
display.show(letter)```