如何在不使用python3的情况下添加新行?

2024-04-20 07:45:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一名初学者,我正在尝试制作一个程序,输入一个名称并以*(星形)模式打印名称, 我定义了函数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())

Tags: or名称sizelendef模式finalcurve
1条回答
网友
1楼 · 发布于 2024-04-20 07:45:17

如果要像AAAB那样打印它们,则需要更改逻辑。因为当您调用单个A()时,python将使用您使用的所有newlines来打印它。您可以执行以下操作

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 + '*' + ' ' * space1

        elif x == midline:
            s = ' ' * space1 + '*' + '*' * space2 + '*' + ' ' * space1
            space2 = space2 + 2

        else:
            s = ' ' * space1 + '*' + ' ' * space2 + '*' + ' ' * space1
            space2 = space2 + 2
        space1 = space1 - 1
        final = final + '\n' + s
    return final

a1 = A().split('\n')
a2 = A().split('\n')

for i in range(len(a1)):
    print(a1[i] + a2[i])

我在这里修改了A()。在这里,我取了这封信,然后用\n把它们分开。然后连接每一行并打印。创建字母时,请在一行中打印的最后一颗星后使用相同数量的空格

enter image description here

相关问题 更多 >