How to write a python program that read the input and convert it to pre-defined 14-segments characters with turtle graphics?
-
30-09-2019 - |
문제
I have written the turtle graphics functions that define the fourteen segments, and the functions that assemble these segments to form characters, e.g.
def MethodA (width) :
top_stroke(width)
middle_stroke(width)
left_stroke
right_stroke(width)
I have all the definitions ready, but how do I let python to read an input and convert the characters of the input into the fourteen segments forms, which I defined previously?
Idealy, if I enter "Pizza", the program should produce the output of the characters 'PIZZA' in the fourteen-segments form.
Any suggestions are welcomed and appreciated. Thanks,
해결책
You could define a dictionary with function references like this:
Characters = {
'A': MethodA,
'B': MethodB,
# ...
}
Then, for a string s
:
s = "Pizza"
for c in s:
c = c.upper() # to fold lowercase into upper case
if c in Characters:
Characters[c](width)
This code works using Characters[c]
to look up the function reference in the dictionary, then the (width)
causes the function to be called (with a width
parameter, as the function expects).