0

I'm creating a recursive function that creates n lines of asterisk. I do not have problems on writing code, but just am wondering why None appears in my output.

Here is my code:

def recursive_lines(n):
    for n in range(0,n):
        print ('*' + ('*'*n)) # Print asterisk
    
print(recursive_lines(5)) # Enter an integer here

And this is the result:

*
**
***
****
*****
None

I don't think I used any int(print()) kind of statement here.. Then why does this error keep appearing?

han zo
  • 11
  • 4
  • 1
    Does this answer your question? [What is the purpose of the return statement? How is it different from printing?](https://stackoverflow.com/questions/7129285/what-is-the-purpose-of-the-return-statement-how-is-it-different-from-printing) – Arkistarvh Kltzuonstev Nov 20 '22 at 04:46
  • As an aside, your function isn't actually recursive because it doesn't call itself. – Woodford Nov 20 '22 at 05:18

3 Answers3

0

You are printing out recursive_lines(5), but inside the function, you are already printing the values. Simply remove the print that is around recursive_lines(5)

MrDiamond
  • 958
  • 3
  • 17
0

The None is printing because you are using print(recursive_lines(5)) even though your function is not returning anything. Remove the print statement while calling the function.

def recursive_lines(n):
    for n in range(0,n):
        print ('*' + ('*'*n)) 
    
recursive_lines(5) 
0

Your function isn't returning anything. By default it'll return none. And if you don't want it to print none you can just not print recursive_lines(5). If you add a return 0 statement it'll return 0 instead of none

Ikura
  • 188
  • 1
  • 11