+ 2

My code does not add up as in value but it rather adds up as strings

name =(input()).split() n = "" for i in name: if (int(i)%2==0): n += i print(n)

28th May 2020, 8:56 AM
Papa Kwasi Ansah Asare
Papa Kwasi Ansah Asare - avatar
4 Réponses
+ 4
Because in n+=i i is string not int
28th May 2020, 9:01 AM
Abhay
Abhay - avatar
+ 4
Kwasi Anash, additional to the posts already given, i want point to 3 things (there is nothing wrong, only to mention it): (1) It is not necessary to split input to a list, you can iterate also over a string. (2) (input()) does not need to be enclosed by parenthesis (3) As i suppose that the input should be a number, it would be a good idea to mention what user should enter. It is also a bit confusing if a variable is named "name", but will contain a number.
28th May 2020, 11:31 AM
Lothar
Lothar - avatar
+ 1
Do you want to count even digits? int(i) only creates an int of i for that one line. i itself will remain a string. This change should work: name =(input()).split() n = "" for i in name: i = int(i) if (i%2==0): n += i print(n)
28th May 2020, 9:03 AM
HonFu
HonFu - avatar