我试图像这样这个问题:
I have attempted this problem like this :
a = input("Enter number : ")
s = 3
w = 1
while a>0:
digit=a%10
if n%2 == 0:
p = p*digit
else:
s = s+digit
a=a/10
n=n+1
print "The sum is",s
它完美的作品,即使数字不但是对于数字奇没有像234它显示的总和为6和产品3
it works perfectly for even no of digits but for odd no of digits like for 234 it shows the sum as 6 and product 3
没有的明确的循环:
import operator
from functools import reduce # in Python 3 reduce is part of functools
a = input("Enter number : ")
lst = [int(digit) for digit in a]
add = sum(lst[1::2])
mul = reduce(operator.mul, lst[::2],1)
print("add =",add,"mul =",mul,"result =",add+mul)
产地:
Enter number : 234
add = 3 mul = 8 result = 11