Hi, I just joined today and i need some help with python. I just started using python not to long ago in my intro programming class. We have an assignment coming up and i need some help. How do you get the sum of an integer. ie: you put in 123 and the sum is 6. I cant seem to figure it out. how do i go about doing that?

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.

Hi, I just joined today and i need some help with python. I just started using python not to long ago in my intro programming class. We have an assignment coming up and i need some help. How do you get the sum of an integer. ie: you put in 123 and the sum is 6. I cant seem to figure it out. how do i go about doing that?

def main():
n=int(raw_input("enter the number"))
reminder=n%10
quotient=n/10
sum=reminder

while(quotient>=10):
reminder=quotient%10
quotient=quotient/10
sum=sum+reminder
if(quotient<10):
sum=sum+quotient
print 'the given number',n
print "the sum of integer is",sum

if __name__ == '__main__':
main()

Here you go:

print reduce( lambda x,y: x+y, [int(i) for i in list(raw_input("Enter integer:"))] )

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

Here you go:

print reduce( lambda x,y: x+y, [int(i) for i in list(raw_input("Enter integer:"))] )

Nice bit of Python !!

Nice bit of Python !!

Thanks. Actually, I realized that it should have been much more simpler:

sum([int(i) for i in list(raw_input("Enter integer:"))])

Sometimes the obvious isn't obvious to me :)

Even simpler:

sum([int(i) for i in raw_input("Enter integer: ")])

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.

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.

well, computer science isnt bad. heck its the only thing im good at schooling wise. im just waiting till i can create actual programs. not these little find the binary value or make a menu with 4 options on it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.