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?

5th May 2019, 7:51 PM
Oscar Schyum
Oscar Schyum - avatar
5 Respuestas
+ 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.
5th May 2019, 8:11 PM
Anna
Anna - avatar
+ 2
Change print to return in your function and all will be 👌.
5th May 2019, 8:00 PM
Dmytro Novak
Dmytro Novak - avatar
+ 1
Yes, it's always from the inside to the outside
5th May 2019, 8:17 PM
Anna
Anna - avatar
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.
5th May 2019, 8:00 PM
Oscar Schyum
Oscar Schyum - avatar
0
so in the text: print(print(string)) the part: print(string) is run first?
5th May 2019, 8:15 PM
Oscar Schyum
Oscar Schyum - avatar