[Solved] How can I remove the ‘none’ out of the output? [closed]


Here’s a modification on @schwobasggl’s answer, using f-strings that works in Python 3.6+

def up_and_down(n):  # 1,2, ..., n, ..., 2, 1
    return (*range(1, n), *range(n, 0, -1))


def diamond(n, pad='#', fill="*"):
    w = (2 * n) - 1

    for i in up_and_down(n):
        # you can also use the below, but it's slightly less performant:
        # fill_w = len(up_and_down(i))
        fill_w = i * 2 - 1

        print(f'{fill * fill_w:{pad}^{w}}')

It should end up with the same result:

>>> diamond(7)

######*######
#####***#####
####*****####
###*******###
##*********##
#***********#
*************
#***********#
##*********##
###*******###
####*****####
#####***#####
######*######

If you want to print a triangle (the top half of the diamond), you can iterate over only the first half of up_and_down in the outer loop. Another way is to just iterate over range(1, n+1) – that is 1, 2, 3 for n=3 – as shown below:

def triangle(n, pad='#', fill="*"):
    w = (2 * n) - 1

    for i in range(1, n + 1):
        print(f'{fill * (i * 2 - 1):{pad}^{w}}')

It looks like that should now work to print just the top half of the diamond above:

>>> triangle(7)

######*######
#####***#####
####*****####
###*******###
##*********##
#***********#
*************

Links for reference

6

solved How can I remove the ‘none’ out of the output? [closed]