0
I gett an unwanted "none" as an output while using fuction in for lopp
My code: people = ["oscar", "carl", "jenny"] def add_and(name): print(name + " and") for x in people: print(add_and(x)) output: > oscar and > none > carl and > none > jenny and > none why does this happen, where does "none" come from?
5 odpowiedzi
+ 4
print(function) prints the return value of the function. The return value of the print() function is None. Thus, print(print(string)) prints string and None.
+ 2
Change print to return in your function and all will be 👌.
+ 1
Yes, it's always from the inside to the outside
0
okej so i have solved this but will leave the post up if anyone is curious.
The error occurs because print() is used twice. If we look att the first loop then x will be "oscar". What im telling the program is:
print(add_and(x)) --> print(print(oscar + " and")).
It's not necessary to use print() twice. But I dont really undersand why " oscar and" and "none" is the output.
0
so in the text: print(print(string))
the part: print(string) is run first?