我是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
但是我想知道如何创建一个可以接受用户输入并以上面的样式打印它。 我已经尝试了几个小时而无法提出解决方案,看到其他人可以做到/做到这一点会很棒。我认为当字母不适合屏幕时会变得更难,所以我只想打印10个字母。 感谢
答案 0 :(得分:3)
假设:您构建的字母全部且所有字母的行数相同。
在这种情况下,您可以构建字典,例如:
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)
或者把它们放在一起:
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)
print()
这会产生:
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
答案 1 :(得分:2)
首先,您必须像以前一样手动制作字母,
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)
答案 2 :(得分:2)
如果没有超级复杂,你需要硬编(即静态定义)每个字母作为字符串列表,类似于你用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
答案 3 :(得分:0)
我找到了一个不太复杂(对我而言)的答案。我不明白如何使用zip或map,但这种方式可以解决它们。我可以理解代码在做什么。
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('')