Sum of odd digits in a string python
the question asks to find the sum of odd numbers when given a string of numbers. so for example, if we are given "123" we should get the sum of 4. Show This is my attempt and it returns '4' which is incorrect
I've also tried converting s into integers but it keeps giving me the "int is not iterable" error
asked Dec 1, 2019 at 7:07
This should work for you:
or if you want to keep your first attempt, you should iterate through your
answered Dec 1, 2019 at 7:17
AyoubAyoub 1,40916 silver badges24 bronze badges I like using a regex approach here. We can apply the regex pattern
This prints:
answered Dec 1, 2019 at 7:24
Tim BiegeleisenTim Biegeleisen 468k24 gold badges253 silver badges330 bronze badges Your first approach does not work because what you're actually adding to the sum are 'indices' of
Now at this point you may wonder
and you'll be absolutely right. Luckily, Python comes with comprehensions which allow us to shorten some of the The result you may get will be something like this:
With Python 3.8 you can also make use of brand new walrus operator:
The problem with your second approach is already explained by the Python interpreter itself. You've converted your string to the integer, and there's no built-in way too loop over integers. You'll need to find and implement your own way to loop over integer's digits (hint: use remainder modulo 10). answered Dec 1, 2019 at 7:48
PoneworPonewor 1012 silver badges5 bronze badges The reason your program is not working is because of the line:
here, you are taking all values of i from 0 to the length of the string (exclusive of length). This means, you are adding up all the odd numbers from 0 to the length of the String. Instead do this:
this will give you each character in the string as i. Now:
will convert it to int. Finally :
the final code will be:
answered Dec 1, 2019 at 7:54
if you use 'range(len(s))' in for loop instead of 's', it will give you [0,1,2,3,4] which is 0 to length of the '12345. Therefore, it is required to change the range by just s, then it will give you ['1','2','3','4','5'] which is char elements of string '12345'. Don't forget to change the type of i to int! answered Dec 1, 2019 at 7:47
Here is a better and more readable solution:
answered Dec 1, 2019 at 7:42
moh80smoh80s 7551 gold badge7 silver badges21 bronze badges 1 How do you find the sum of odd digits in Python?Working :. Step 1: Read the integer input.. Step 2: initialize s and set it to zero.. Step 3: using modulo with 10 logic we will get last digit of the number.. Step 4: Do this operation by using while loop.. Step 5: Check whether the number is odd or not , if number is odd add to the result.. Step 6: Print the resultant sum.. How do you find the sum of digits in an odd place?Approach: First, calculate the reverse of the given number. To the reverse number we apply modulus operator and extract its last digit which is actually the first digit of a number so it is odd positioned digit. The next digit will be even positioned digit, and we can take the sum in alternating turns.
How do you add odd items to a list in Python?Practical Data Science using Python
We have to find sum of all odd elements from the list. So, if the input is like nums = [5,7,6,4,6,9,3,6,2], then the output will be 24 because 5+7+9+3 = 24. return the sum of elements in l by passing l into sum() function.
|