1

I am making a morse code translator for the micro;bit. I have button A as the dot, button B for the dash and both together for enter. The problem is I don't know how lists work so I'm not sure what to do.

Alist = ["ButtonA", "ButtonB", "ButtonAB"]
Blist = ["ButtonB", "ButtonA", "ButtonA", "ButtonA", "ButtonAB"]
Clist = ["ButtonB", "ButtonA", "ButtonB", "ButtonA", "ButtonAB"]
Dlist = ["ButtonB", "ButtonA", "ButtonA", "ButtonAB"]
Elist = ["ButtonA", "ButtonAB"]
Flist = ["ButtonA", "ButtonA", "ButtonB", "ButtonA", "ButtonAB"]
Glist = ["ButtonB", "ButtonB", "ButtonA", "ButtonAB"]
Hlist = ["ButtonA", "ButtonA", "ButtonA", "ButtonA", "ButtonAB"]
Ilist = ["ButtonA", "ButtonA", "ButtonAB"]
Jlist = ["ButtonA", "ButtonB", "ButtonB", "ButtonB", "ButtonAB"]
Klist = ["ButtonB", "ButtonA", "ButtonB", "ButtonAB"]
Llist = ["ButtonA", "ButtonB", "ButtonA", "ButtonA", "ButtonAB"]
Mlist = ["ButtonB", "ButtonB", "ButtonAB"]
Nlist = ["ButtonB", "ButtonA", "ButtonAB"]
Olist = ["ButtonB", "ButtonB", "ButtonB", "ButtonAB"]
Plist = ["ButtonA", "ButtonB", "ButtonB", "ButtonA", "ButtonAB"]
Qlist = ["ButtonB", "ButtonB", "ButtonB", "ButtonA", "ButtonAB"]
Rlist = ["ButtonA", "ButtonB", "ButtonA", "ButtonAB"]
Slist = ["ButtonA", "ButtonA", "ButtonA", "ButtonAB"]
Tlist = ["ButtonB", "ButtonAB"]
Ulist = ["ButtonA", "ButtonA", "ButtonB", "ButtonAB"]
Vlist = ["ButtonA", "ButtonA", "ButtonA", "ButtonB", "ButtonAB"]
Wlist = ["ButtonA", "ButtonB", "ButtonB", "ButtonAB"]
Xlist = ["ButtonB", "ButtonA", "ButtonA", "ButtonB", "ButtonAB"]
Ylist = ["ButtonB", "ButtonA", "ButtonB", "ButtonB", "ButtonAB"]
Zlist = ["ButtonB", "ButtonB", "ButtonAB"]
ButtonA = input.button_is_pressed[Button.A]
ButtonB = input.is_button_pressed[Button.B]
ButtonAB = input.button_is_pressed[Button.AB]

if Alist:
    basic.show_string("A")

I tried an if statement to check if it's requirements were met and if so, to print the letter A. It's probably simple but I'm not at that skillset yet.

1 Answers1

1

You a 'mapping' from one thing to another, so a Dictionary is a natural approach. Along the lines as shown below. A tuple of the button presses is used as the key to the Dictionary.:

morse = {('bA', 'bB', 'bAB'): 'a',
         ('bB', 'bA','bA','bA','bAB') : 'b'
         }
#and so on for all the letters
entry = ('bA', 'bB', 'bAB')

print(morse.get(entry, 'unknown'))

gives

a

user19077881
  • 3,643
  • 2
  • 3
  • 14