How to write a python program that read the input and convert it to pre-defined 14-segments characters with turtle graphics?

StackOverflow https://stackoverflow.com/questions/4137040

  •  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).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top