+ 2

Pls what is wrong with this code

For 'That's odd' challenge a = input().split() b = '' for x in a: if int(x) % 2 == 0: b += '+' + x c = eval(b) print(c)

27th Aug 2020, 2:04 PM
Abdulhameed Oladapo
12 Respostas
27th Aug 2020, 2:13 PM
Julia Shabanova
Julia Shabanova - avatar
+ 6
Abdulhameed Print value of variable <b> before you send it to eval(). Once you do that, you will understand what the problem is👍
27th Aug 2020, 2:19 PM
Ipang
+ 6
Abdulhameed I just fixed: eval, str+int, len(b)==0
27th Aug 2020, 2:28 PM
Julia Shabanova
Julia Shabanova - avatar
+ 3
It is working on my windows IDE and on sololearn playground
27th Aug 2020, 2:11 PM
Abdulhameed Oladapo
+ 3
Ipang Thank you I understand now👍
27th Aug 2020, 2:28 PM
Abdulhameed Oladapo
+ 2
M.d Nasif Your code worked, Thanks But I don't understand why mine did not work can you explain to me Maybe I did not get the question correctly
27th Aug 2020, 2:22 PM
Abdulhameed Oladapo
+ 2
Thanks guys I was able to solve But can anyone explain to me why mine did not work
27th Aug 2020, 2:25 PM
Abdulhameed Oladapo
+ 2
Why people are suggesting alternative solutions lol ! When you input something like 40 40 and even 45 40 , that works fine ,since b isn't empty when evalutes but when input is something like 45 45 then b is just an empty string which isn't a valid python expression and so you get the error ,now you can google more about what python expressions are considered valid by eval function!
27th Aug 2020, 2:31 PM
Abhay
Abhay - avatar
+ 1
The error it is showing is Line 6 c = eval(b)
27th Aug 2020, 2:09 PM
Abdulhameed Oladapo
+ 1
here is my solution, hope it will help. https://code.sololearn.com/czrrnkkXuLY3/?ref=app
27th Aug 2020, 2:17 PM
Shadoff
Shadoff - avatar
+ 1
Addulhameed b += '+' + x char can't be added to char b is char variable '+' is char leteral x is also char variable not considered as valid operands for + operator. DHANANJAY PATEL
29th Aug 2020, 3:12 AM
DHANANJAY PATEL
DHANANJAY PATEL - avatar
0
Here is my solution n = int(input()) l = [int(input()) for i in range(n)] print(sum(list(filter(lambda x: x % 2 == 0, l))))
29th Aug 2020, 4:39 AM
Jonathan Alvarado
Jonathan Alvarado - avatar