由字母组成的字母
Letters Made From Letters
我是 python(版本 3.4.)的新手,我想知道如何编写与此类似的代码:
#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])
我当前代码的输出如下所示:
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
但我想知道如何制作一个可以接受用户输入并以上面的样式打印的。
我已经尝试了几个小时,但无法提出解决方案,很高兴看到其他人 do/have 是如何做到的。我认为当屏幕上放不下字母时会变得更加困难,所以我只想打印 10 个字母。
谢谢
Assumption: you have all the letters constructed and that all letters have the same number of rows.
在这种情况下,您可以构造一个字典,例如:
ascii_art = { 'B': B, 'E': E, 'N': N }
当然在现实生活中,你构建一个包含所有字母、可能还有空格、数字等的字典
现在您可以将字符串作为输入:
text = input('Enter text? ')
接下来我们将字符串映射到一个可迭代的字母上:
chars = map(ascii_art.get,text)
最后我们将它们放入一个 zip 中并打印出来:
for d in zip(*chars):
print(*d)
或者把它们放在一起:
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)
如果你想限制输出到每行 10 个字符,你可以将代码更改为:
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)
结果为:
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
我们可以通过添加一个额外的语句来每行添加一个空行:
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)
<b>print()</b>
这会生成:
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
首先你必须像以前一样手动制作字母表,
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]
对每个字母都这样做。 [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)
在不变得超级复杂的情况下,您需要将每个字母硬核化(即静态定义)为字符串列表,类似于您对 B、E 和 N 的处理方式。
然后你构建一个字典,将每个字母映射到相应的列表:
>>> 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"]
... }
本例只包含字母b、e、n的定义,其他的你需要自己添加定义。确保所有列表的长度相同。一旦你完成了这些,你就可以使用这个字典来显示你从用户输入中得到的任何字母序列。演示:
>>> 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
我找到了一个不太复杂(对我来说)的答案。我不明白如何使用 zip 或 map,但是这种方式与 out them.And 我可以理解代码在做什么。
A =[" A ", #CREATING LIST OF LETTERS
" A A ",
" A A ",
"AAAAAAA ",
"A A ",
"A A ",
"A A "]
以这种方式创建字母表
然后:
#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('')
我是 python(版本 3.4.)的新手,我想知道如何编写与此类似的代码:
#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])
我当前代码的输出如下所示:
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
但我想知道如何制作一个可以接受用户输入并以上面的样式打印的。 我已经尝试了几个小时,但无法提出解决方案,很高兴看到其他人 do/have 是如何做到的。我认为当屏幕上放不下字母时会变得更加困难,所以我只想打印 10 个字母。 谢谢
Assumption: you have all the letters constructed and that all letters have the same number of rows.
在这种情况下,您可以构造一个字典,例如:
ascii_art = { 'B': B, 'E': E, 'N': N }
当然在现实生活中,你构建一个包含所有字母、可能还有空格、数字等的字典
现在您可以将字符串作为输入:
text = input('Enter text? ')
接下来我们将字符串映射到一个可迭代的字母上:
chars = map(ascii_art.get,text)
最后我们将它们放入一个 zip 中并打印出来:
for d in zip(*chars):
print(*d)
或者把它们放在一起:
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)
如果你想限制输出到每行 10 个字符,你可以将代码更改为:
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)
结果为:
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
我们可以通过添加一个额外的语句来每行添加一个空行:
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)
<b>print()</b>
这会生成:
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
首先你必须像以前一样手动制作字母表,
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]
对每个字母都这样做。 [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)
在不变得超级复杂的情况下,您需要将每个字母硬核化(即静态定义)为字符串列表,类似于您对 B、E 和 N 的处理方式。
然后你构建一个字典,将每个字母映射到相应的列表:
>>> 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"]
... }
本例只包含字母b、e、n的定义,其他的你需要自己添加定义。确保所有列表的长度相同。一旦你完成了这些,你就可以使用这个字典来显示你从用户输入中得到的任何字母序列。演示:
>>> 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
我找到了一个不太复杂(对我来说)的答案。我不明白如何使用 zip 或 map,但是这种方式与 out them.And 我可以理解代码在做什么。
A =[" A ", #CREATING LIST OF LETTERS
" A A ",
" A A ",
"AAAAAAA ",
"A A ",
"A A ",
"A A "]
以这种方式创建字母表 然后:
#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('')