+ 1

PYTHON3 Function ad for loop

def even(): for i in range(10): i=i**2 if i%2==0: print(i) even() evens=[even(),'abc'] print(evens[0]) I have written this program to better understand the function but I cannot find a way to not print two times the result. Can anyone help me?

20th Nov 2017, 7:26 AM
Michael Nervo
Michael Nervo - avatar
2 ответов
+ 3
Your code has two instances of even() being called, and your even function prints the output. This is why you're seeing the output twice. You could return i instead, so your last print function only displays the result once. In the above solution, it could also be said that line 6 isn't needed. *edit* After looking more carefully, I realise you'd be returning the first value, so assumed you want to return a list of values. I've modified the code and included below, just ask if there's anything you don't understand. https://code.sololearn.com/cXS8pZ6KCOdr/
20th Nov 2017, 8:28 AM
Steve
Steve - avatar
0
Yes! Is what i meant to do. Thanks Steve for the explanation and the source code👍🏻
20th Nov 2017, 8:55 AM
Michael Nervo
Michael Nervo - avatar