简体   繁体   English

如何在 python3 中不使用 \n 添加新行?

[英]How to Add new line without using \n in python3?

I am a beginner, I am trying To make a program which inputs a name and prints name in * (star) pattern, I defined function A and B which return * pattern Of A and B, But when i combine them to print they print in new line, I want to print them in same line.我是初学者,我正在尝试制作一个输入名称并以* (星号)模式打印名称的程序,我定义了 function A 和 B,它们返回*模式的 A 和 B,但是当我将它们组合起来打印时,它们会打印在新行中,我想在同一行中打印它们。 I tried print argument end='' or sep='' but it is not working.我尝试了 print argument 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())

If you want to print them like, AA or AB then you would need to change the logic.如果您想像AAAB一样打印它们,那么您需要更改逻辑。 Because when you call a single A() python will print it with all the newlines you have used.因为当您调用单个A() python 将使用您使用的所有newlines打印它。 You can do something like the following您可以执行以下操作

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])

I have modified A() here.我在这里修改A() Here I have fetched the letter then split them using \n .在这里,我获取了这封信,然后使用\n将它们拆分。 Then Concatenate each of the lines and print.然后连接每一行并打印。 When creating a letter, use the same number of spaces after the last star you have printed in a line.创建字母时,在一行中打印的最后一个星号后使用相同数量的空格。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM