-6

I was supposed to make a diamond out of this and I have successfully done so but I have a problem. Here is my code

k = 7
def triangle(k):
    a = (2*k) -1
    k = 3
    for x in range(0,a+1):
        if x % 2 != 0:
            f = int((a - x)/2)
            g = f"{'#'*f}{'*'*x}{'#'*f}"
            print(g)
def diamond(k):
    a = (2 * k) - 1
    k = 3
    for x in reversed(range(0, a)):
        if x % 2 != 0:
            f = int((a - x) / 2)
            g = f"{'#' * f}{'*' * x}{'#' * f}"
            print(g)

l = f"{triangle(k)}\n{diamond(k)}"
print(l)

Along with the diamond, there are also two 'none' appearing underneath like this in the output..

######*######
#####***#####
####*****####
###*******###
##*********##
#***********#
*************
#***********#
##*********##
###*******###
####*****####
#####***#####
######*######
None
None

How can I print the output without including the none in the output??

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Nutnicha
  • 335
  • 1
  • 10
  • 2
    Neither functions return anything, so capturing and printing their calls makes little sense – DeepSpace Oct 12 '21 at 14:03
  • `triangle(k)` and `diamond(k)` both don't return anything (i.e. they return `None`). Instead, they contain print statements. So when you do `print(l)` you're printing `"None\nNone"` – Green Cloak Guy Oct 12 '21 at 14:03
  • 1
    What's the point of `l = f"{triangle(k)}\n{diamond(k)}"; print(l)`? Calling `triangle` and `diamond` already prints the results – ForceBru Oct 12 '21 at 14:04
  • I tried not printing it but the result is wrong though, like the figure is distorted when I try it... is there any part of my code that I should change to make sure the 'none' doesnt get printed?? – Nutnicha Oct 12 '21 at 14:08
  • `triangle(k) ; print() ; diamond(k)` – Tomerikoo Oct 12 '21 at 23:19
  • Does this answer your question? [Why is this printing 'None' in the output?](https://stackoverflow.com/questions/28812851/why-is-this-printing-none-in-the-output) – Tomerikoo Oct 12 '21 at 23:20

2 Answers2

0

Calling the functions already prints the output. Printing the return values (None) makes little sense. That being said, this can be tremendously simplified:

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

def diamond(n):
    for i in up_and_down(n):
        print((n-i)*'#', *('*' for _ in up_and_down(i)), (n-i)*'#', sep='') 

>>> diamond(7)
######*######
#####***#####
####*****####
###*******###
##*********##
#***********#
*************
#***********#
##*********##
###*******###
####*****####
#####***#####
######*######
user2390182
  • 72,016
  • 6
  • 67
  • 89
  • where should I put this in my code though?? Like can u please copy and paste my code and then show where to put your piece of code please?? – Nutnicha Oct 12 '21 at 14:22
0

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

rv.kvetch
  • 9,940
  • 3
  • 24
  • 53