【发布时间】:2021-06-23 11:38:45
【问题描述】:
我是初学者,我正在尝试制作一个程序,该程序可以输入名称并以*(星号)模式打印名称,
我定义了函数 A 和 B,它们返回 A 和 B 的 * 模式,但是当我将它们组合起来打印时
他们在新行中打印,我想在同一行中打印它们。我试过打印参数end='' 或sep='' 但是
它不工作。
def A(size = 10):
final = str()
height = size
breath = size
space1 = breath
space2 = 1
midline = int(height/2) + 2 #adjust the position of miline
for x in range(1,height+1):
if x == 1 :
s = ' '*space1 + '*'
elif x == midline :
s = ' '*space1 + '*' + '*'*space2 + '*'
space2 = space2 + 2
else :
s = ' '*space1 + '*' + ' '*space2 + '*'
space2 = space2 + 2
space1 = space1 - 1
final = final + '\n' + s
return final
def B(size=10):
final = str()
height = size
breath = size
space = breath - 2
curve = 3
for x in range(1,height+1):
if x == (height//2 + 1):
s = '*'*(breath - curve)
s = s + ' '*(breath-len(s))
elif x == 1 or x == height:
s = '*' * (breath-curve)
s = s + ' '*(breath-len(s))
elif x == 2 or x == (height-1):
s = '*'+ ' '*(breath-curve) + '*'
s = s + ' '*(breath-len(s))
elif x == (height//2 + 1)-1 or x == (height//2 + 1)+1:
s = '*'+ ' '*(breath-curve) + '*'
s = s + ' '*(breath-len(s))
else:
s = '*' + ' '*space + '*'
s = s + ' '*(breath-len(s))
final = final + '\n' + s
return final
print(B())
【问题讨论】:
-
您的意思是希望能够将
A()和B()的输出组合起来打印“AB”,即彼此相邻?
标签: python python-3.x string replace printing