Letters Made From Letters

走远了吗. 提交于 2019-12-24 01:54:12

问题


I am new to python(version 3.4.) and I am wondering how I can make a code similar to this one:

#block letters 
B1 = ("BBBB  ")
B2 = ("B   B ")
B3 = ("B   B ")
B4 = ("BBBB  ")
B5 = ("B   B ")
B6 = ("B   B ")
B7 = ("BBBB  ")
B = [B1, B2, B3, B4, B5, B6, B7]

E1 = ("EEEEE ")
E2 = ("E     ")
E3 = ("E     ")
E4 = ("EEEEE ")
E5 = ("E     ")
E6 = ("E     ")
E7 = ("EEEEE ")
E = [E1, E2, E3, E4, E5, E6, E7]

N1 = ("N    N")
N2 = ("NN   N")
N3 = ("N N  N")
N4 = ("N N  N")
N5 = ("N  N N")
N6 = ("N   NN")
N7 = ("N    N")
N = [N1, N2, N3, N4, N5, N6, N7]
for i in range(7):
    print(B[i], E[i], N[i])

The output of my current code looks like this:

BBBB   EEEEE  N    N
B   B  E      NN   N
B   B  E      N N  N
BBBB   EEEEE  N N  N
B   B  E      N  N N
B   B  E      N   NN
BBBB   EEEEE  N    N

But I want to know how to make one that can take user input and print it in the style above. I have been trying for a few hours and can't come up with a solution, it would be great to see how other people could do/have done it. I think it becomes a lot harder when ther letters do not fit on the screen, so I only want to be able to print 10 letters. Thanks


回答1:


Assumption: you have all the letters constructed and that all letters have the same number of rows.

In that case you can construct a dictionary, like:

ascii_art = { 'B': B, 'E': E, 'N': N }

of course in real life, you construct a dictionary with all letters, and perhaps spaces, digits, etc.

Now you can take an string as input with:

text = input('Enter text? ')

Next we map the string onto an iterable of letters:

chars = map(ascii_art.get,text)

and finally we put these into a zip and print that:

for d in zip(*chars):
    print(*d)

Or putting it all together:

ascii_art = { 'B': B, 'E': E, 'N': N }
text = input('Enter text? ')
chars = map(ascii_art.get,text)
for d in zip(*chars):
    print(*d)

In case you want to limit the output to 10 chars per line, you can alter the code to:

ascii_art = { 'B': B, 'E': E, 'N': N }
text = input('Enter text? ')
for i in range(0,len(text),10):
    chars = map(ascii_art.get,text[i:i+10])
    for d in zip(*chars):
        print(*d)

This results into:

Enter text? BEBEBEBBEBEENNNENNNN
BBBB   EEEEE  BBBB   EEEEE  BBBB   EEEEE  BBBB   BBBB   EEEEE  BBBB  
B   B  E      B   B  E      B   B  E      B   B  B   B  E      B   B 
B   B  E      B   B  E      B   B  E      B   B  B   B  E      B   B 
BBBB   EEEEE  BBBB   EEEEE  BBBB   EEEEE  BBBB   BBBB   EEEEE  BBBB  
B   B  E      B   B  E      B   B  E      B   B  B   B  E      B   B 
B   B  E      B   B  E      B   B  E      B   B  B   B  E      B   B 
BBBB   EEEEE  BBBB   EEEEE  BBBB   EEEEE  BBBB   BBBB   EEEEE  BBBB  
EEEEE  EEEEE  N    N N    N N    N EEEEE  N    N N    N N    N N    N
E      E      NN   N NN   N NN   N E      NN   N NN   N NN   N NN   N
E      E      N N  N N N  N N N  N E      N N  N N N  N N N  N N N  N
EEEEE  EEEEE  N N  N N N  N N N  N EEEEE  N N  N N N  N N N  N N N  N
E      E      N  N N N  N N N  N N E      N  N N N  N N N  N N N  N N
E      E      N   NN N   NN N   NN E      N   NN N   NN N   NN N   NN
EEEEE  EEEEE  N    N N    N N    N EEEEE  N    N N    N N    N N    N

We can add an empty line per row, by adding a single extra statement:

ascii_art = { 'B': B, 'E': E, 'N': N }
text = input('Enter text? ')
for i in range(0,len(text),10):
    chars = map(ascii_art.get,text[i:i+10])
    for d in zip(*chars):
        print(*d)
    print()

this generates:

Enter text? BBBEEEEEEENNNNN
BBBB   BBBB   BBBB   EEEEE  EEEEE  EEEEE  EEEEE  EEEEE  EEEEE  EEEEE 
B   B  B   B  B   B  E      E      E      E      E      E      E     
B   B  B   B  B   B  E      E      E      E      E      E      E     
BBBB   BBBB   BBBB   EEEEE  EEEEE  EEEEE  EEEEE  EEEEE  EEEEE  EEEEE 
B   B  B   B  B   B  E      E      E      E      E      E      E     
B   B  B   B  B   B  E      E      E      E      E      E      E     
BBBB   BBBB   BBBB   EEEEE  EEEEE  EEEEE  EEEEE  EEEEE  EEEEE  EEEEE 

N    N N    N N    N N    N N    N
NN   N NN   N NN   N NN   N NN   N
N N  N N N  N N N  N N N  N N N  N
N N  N N N  N N N  N N N  N N N  N
N  N N N  N N N  N N N  N N N  N N
N   NN N   NN N   NN N   NN N   NN
N    N N    N N    N N    N N    N



回答2:


Without getting super sophisticated, you need to hardcore (i.e. statically define) each letter as a list of strings, similar to how you did it with B, E and N.

Then you build a dictionary that maps each letter to the corresponding list:

>>> letters = {
...     'b': ["BBBB  ", "B   B ", "B   B ", "BBBB  ", "B   B ", "B   B ", "BBBB  "],
...     'e': ["EEEEE ", "E     ", "E     ", "EEEEE ", "E     ", "E     ", "EEEEE "],
...     'n': ["N    N", "NN   N", "N N  N", "N N  N", "N  N N", "N   NN", "N    N"]
... }

This example only contains the definitions for the letters b, e and n, you need to add the definitions of all the others yourself. Make sure all the lists have the same length. Once you are done with that, you can use this dictionary to display any sequence of letters you get from user input. Demo:

>>> name = raw_input('enter your name: ')
enter your name: Ben
>>>
>>> for row in zip(*[letters[x.lower()] for x in name]):
...     print ''.join(row)
... 
BBBB  EEEEE N    N
B   B E     NN   N
B   B E     N N  N
BBBB  EEEEE N N  N
B   B E     N  N N
B   B E     N   NN
BBBB  EEEEE N    N



回答3:


First you'd have to manually make the alphabet as you did before,

N1 = ("N    N")
N2 = ("NN   N")
N3 = ("N N  N")
N4 = ("N N  N")
N5 = ("N  N N")
N6 = ("N   NN")
N7 = ("N    N")
N = [N1, N2, N3, N4, N5, N6, N7]

Do that for each letter. [a-z]

# Now to let user input print your alphabet we will use a dictionary
# The the key is the letter and value is the printable  array
d = {'a':A,'b':B, ... , 'z':Z }

# Let's ask for user input:
line = input('What do you want to print> ')

# Now lets print what the user said in our alphabet
# iterate through the input and print it

sentence = map(d.get,line)
for letter in zip(*sentence):
    print(*letter)



回答4:


I have found a less complicated(for me) answer. I don't understand how to use zip or map, but this way work with out them.And I can understand what the code is doing.

A =["   A    ", #CREATING LIST OF LETTERS
    "  A A   ",
    " A   A  ",
    "AAAAAAA ",
    "A     A ",
    "A     A ",
    "A     A "]

create the alphabet in this manner then:

#PUTTING LETTERS IN LIST
Alphabet = {"A":A,"B": B, "C": C, "D":D, "E":E ,"F":F, "N": N} 
#ASKER USER FOR INPUT
text = input('Enter text(Only takes A, B, C, D, E, F, N)? ')
#INPUT IN UPPERCASE
text = text.upper()
for i in range(7):
    for letter in text:
        temp = Alphabet[letter]
        print(temp[i],end="")
    print('')


来源:https://stackoverflow.com/questions/43893431/letters-made-from-letters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!