0

Want to write a function that outputs a pyramid of stars.

This function must be called with a print statement.

My solution results in None displayed.

def tree(n): 
    for i in range(n): 
        for j in range(n-i-1): 
            print(" ", end="")
        for j in range(2*i+1): 
            print('*', end="")
        print('\r')
        
print(tree(5))
    *
   ***
  *****
 *******
*********
None

How do I get rid of the None?

I tried using a return statement with no success:

def tree(n): 
    for i in range(n): 
        a=((n - (i + 1)) * " " + ((2*i)+1) * '*')
    
    return a
    
print(tree(5))

returns

*********

Is the return statement the right approach?

Calling the function without the print statement is not an option :)

Thanks.

  • How many `print` calls do you have? What do you expect each to output and why? – deceze Dec 09 '22 at 15:42
  • Your attempt at `return` simply doesn't produce the same string at all. Instead of printing in your function, concatenate to a string each time (`s = ''` `for ...: s += ...`). Then return that at the end of the loop. – deceze Dec 09 '22 at 15:44

0 Answers0