0

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?

7
Contributors
10
Replies
11
Views
8 Years
Discussion Span
Last Post by Zelores
0

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.

0

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()

0

Here you go:

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

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
0

Here you go:

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

Nice bit of Python !!

0

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 :)

0

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.

0

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.

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.