How can I make this code print the square of each even numbers in the list ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can I make this code print the square of each even numbers in the list ?

""" Use a list comprehension to square each even number in the input""" values=input("Enter the values:") x=[i for i in values.split(",") if int(i)%2!=0 i*i] print((",").join(x))

22nd Jun 2017, 5:18 AM
Masquerade
Masquerade - avatar
4 Answers
+ 1
x=[i*i for i in values.split(",") if int(i)%2==0] [ Edit: the title says "even", the body says "odd". Anyway, the change should be simple enough :) ]
22nd Jun 2017, 5:35 AM
Bogdan Sass
Bogdan Sass - avatar
+ 1
There's one more change, much more important - notice the "i*i" at the beginning.
22nd Jun 2017, 6:09 AM
Bogdan Sass
Bogdan Sass - avatar
0
oh yeah, my bad. Guess I just have the change operator to print even or odd . And thanks for the help
22nd Jun 2017, 5:49 AM
Masquerade
Masquerade - avatar
0
haha, yes I saw that. That's the answer I've been looking for and thanked you for haha. Thanks again :-). I am a beginner so these little things, which feels silly to me even gets me scratching my head. :(
22nd Jun 2017, 6:15 AM
Masquerade
Masquerade - avatar