【问题标题】:How to remove the none error from the outputHow to remove the none error from the output
【发布时间】:2022-11-20 12:46:45
【问题描述】:

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?

【问题讨论】:

标签: python python-3.x


【解决方案1】:

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)

【讨论】:

    【解决方案2】:

    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) 
    

    【讨论】:

    • That solved my error. Thank you for your help!
    【解决方案3】:

    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

    【讨论】:

      猜你喜欢
      • 2022-12-28
      • 2022-12-01
      • 2022-12-01
      • 1970-01-01
      • 2022-12-27
      • 2022-12-01
      • 2022-12-02
      • 2022-12-26
      • 2022-12-02
      相关资源
      最近更新 更多