Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove None from the output of a function call

def digits_plus(num):
    for i in range (num+1):
        print (str(i)+"+",end="")

print (digits_plus(3))

Here's what I got returned:

0+1+2+3+None

"None" always exists at the last of the line returned.

I want this returned:

0+1+2+3+

BTW. I'm totally new to programming.I did some research myself, but the answers weren't helpful. They all tell me to remove print but I want the string in the same line so I must also include end="". Help please.

like image 837
Rui Yu Avatar asked Dec 10 '25 19:12

Rui Yu


1 Answers

The reason this is happening to you is that you are printing the result of your function call, in this line here:

print(digits_plus(3))

But your function does not return any value, so it returns None. And None is being printed because you are telling Python to print it. (And it's on the same line as the rest because none of your other prints print a newline.) To solve this, change that line to just:

digits_plus(3)

Your function is doing the printing, so there is no need to also print the function's return value.

(You could also revise your function to return the desired value instead of printing it, which would make it more generally useful.)

like image 101
kindall Avatar answered Dec 12 '25 10:12

kindall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!