You need to convert your integer into a string, iterate the string and sum up the string's numeric characters as integers.
If n is your integer, then str(n) is the string.
If c is the string's numeric character, then int(c) is its numeric/integer value.
Use a for loop to iterate the string.
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
Without the lambda (which you probably haven't covered)
input_str=raw_input("Enter a string of digits ")
##-------------- Simple way --------------------------------------------------
input_list = [int(x) for x in input_str]
print sum(input_list), input_list
##-------------- Check that it is a digit and sum manually -------------------
input_list=[]
errors=0
for chr in input_str:
if chr.isdigit():
input_list.append(int(chr))
else:
print "Only the numbers 0 through 9 are allowed"
errors=1
if not errors:
total=0
for x in input_list:
total += x
print "total =", total, input_list
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
Even simpler:
sum([int(i) for i in raw_input("Enter integer: ")])
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
thanks for all the help everyone! it really helped me. im still a noob when it comes to programming, espically to python. its just to bad that after this quarter i wont be working with python anymore. its all java and c++ lol.
Hehe, every computer student has to go through the pain of C++ or Java programming, so you learn to be humble in the eyes of your trainer. In science, it is more important to solve the problems quickly, so Python rules.
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184