I am trying to find the difference between the sum of the even digits and odd digits in a number. My code only works for just one case which is 412 but it doesnt work for numbers such as 23 and 1203. I dont know why. Here is my code
def digitS(n): even = 0 odd = 0 while (n!=0): for i in range(n): if i % 2 == 0: even = even + (n%10) else: odd = odd + (n%10) n//=10 return even - odd n = 412 print(digitS(n))
Abilogos
4,4772 gold badges16 silver badges37 bronze badges
asked Nov 24, 2020 at 11:22
3
It is kinda obscure to me, that you want to find the difference between odd and even digits or the difference between odd and even positions?
BTW, I assumed you want to count the former -- your code implementing the latter with some issues in designing.
def digit(n): even = 0 odd = 0 while (n != 0): r = n % 10 if r % 2 == 0: even = even + r else: odd = odd + r n //= 10 return even - odd n = 412 print(digit(int(n)))and a more concise response with the aid of strings!
def digit(n): even = [int(i) if int(i) % 2 == 0 else 0 for i in str(n)] odd = [int(i) if int(i) % 2 else 0 for i in str(n)] return sum(even) - sum(odd)answered Nov 24, 2020 at 11:40
javadrjavadr
3601 silver badge8 bronze badges
Just discard for loop.
def digitS(n): even = 0 odd = 0 while (n!=0): if n % 2 == 0: even += (n%10) else: odd += (n%10) n//=10 return even - odd n = 412 print(digitS(n))answered Nov 24, 2020 at 11:33
AbilogosAbilogos
4,4772 gold badges16 silver badges37 bronze badges
1
I've made some edits to your code below. What you have attempted is perfectly right, but the for loop is an obstruction and should be removed. Also, from what I understood, you wanted the difference between the odd and even digit sums. For that, you must return the absolute value of even - odd.
def digits(n): even = 0 odd = 0 while (n!=0): if n % 2 == 0: even += (n%10) else: odd += (n%10) n //= 10 return abs(even - odd) print(digits(412)) # 5 print(digits(23)) # 1answered Nov 24, 2020 at 11:33
tanmay_gargtanmay_garg
3891 silver badge13 bronze badges
2
I think that you don't need for in 5th line, as you only want to iterate digits instead of whole range of numbers. So for example if number is 1203 you want to check 1, 2, 0 and 3, instead of 0, 1, 2, 3 ... 1201, 1202. Also checking digits is already handled in while(n!=0): n//=10, so for isn't needed.
answered Nov 24, 2020 at 11:34
Turn your input to a string, iterate over the elements one by one, check if they are odd or even by turning them to integers et Voila!
def digit(n): even = 0 odd = 0 string = str(n) for i in string: if int(i) % 2 == 0: even += int(i) else: odd += int(i) return even - odd n = 282 print(digit(n)) #12answered Nov 24, 2020 at 11:37
ombkombk
2,0281 gold badge3 silver badges15 bronze badges
I'm not sure what the for loop is meant to do. But here's an example of what you could do instead. Discard the for loop and go through each digit until n == 0.
def digit(n): even = 0 # Sum of even numbers odd = 0 # Sum of odd numbers while (n!=0): digit = n%10 # Get the right most digit of the number if digit % 2 == 0: # Check if the digit is even or odd even = even + digit else: odd = odd + digit n//=10 # Remove the digit from the number return even - odd n = 412 print(digit(n))answered Nov 24, 2020 at 11:39
SandstenSandsten
6873 silver badges16 bronze badges