我想知道是否有人可以幫助我創建字母的星形圖案。我想看到在同一條線上有 UP 或 DOWN。然而,目前,這些字母是在彼此下方列印的,而不是在同一行上的下一個。任何幫助將不勝感激,謝謝。
對 U 函式進行采樣:
def u():
u_str = ''
for row in range (0,7):
for column in range (0,7):
if (((column == 1 or column==5) and row !=6) or ((column==2 or column ==3
or column==4) and row==6)) :
u_str = "*"
else:
u_str = " "
u_str = "\n"
return u_str
向上或向下輸入功能
def up_or_down():
if len(user_letters) ==4 and user_letters == "DOWN":
print(d() o() w() n())
elif len(user_letters) ==2 and user_letters == "UP":
print(u() p())
else:
pass
up_or_down()
uj5u.com熱心網友回復:
首先,我建議您使用 adict來存盤字母 ascii 藝術,這樣您就不會多次重新計算它們:
ascii_letters = {
"D": d(),
"N": n(),
"O": o(),
"P": p(),
"U": u(),
"W": w(),
}
然后創建一個可以水平連接多行字串的函式:
def hcat_strings(*strings):
"""`strings` must have the same number of lines"""
# in a single line
# return "\n".join((" ".join(l) for l in zip(*(s.splitlines() for s in strings))))
strings_lines = (s.splitlines() for s in strings)
lines_as_tuples = zip(*strings_lines)
lines = (" ".join(l) for l in lines_as_tuples)
return "\n".join(lines)
您最終可以按如下方式使用:
from operator import itemgetter
text = "UPPUPU"
letters = itemgetter(*text)(ascii_letters)
print(hcat_strings(*letters))
結果:
* * ***** ***** * * ***** * *
* * * * * * * * * * * *
* * * * * * * * * * * *
* * ***** ***** * * ***** * *
* * * * * * * * *
* * * * * * * * *
*** * * *** * ***
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/505264.html
上一篇:如何在正確位置列印元素總數?
下一篇:如何遍歷資料框以格式化值?
