integer sum

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 11
Reputation: Zelores is an unknown quantity at this point 
Solved Threads: 0
Zelores Zelores is offline Offline
Newbie Poster

integer sum

 
0
  #1
Mar 7th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,297
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: integer sum

 
0
  #2
Mar 7th, 2008
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.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1
Reputation: sakthivelvm is an unknown quantity at this point 
Solved Threads: 1
sakthivelvm sakthivelvm is offline Offline
Newbie Poster

Re: integer sum

 
0
  #3
Mar 11th, 2008
Originally Posted by Zelores View Post
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()
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 17
Reputation: rikxik is an unknown quantity at this point 
Solved Threads: 3
rikxik rikxik is offline Offline
Newbie Poster

Re: integer sum

 
0
  #4
Mar 11th, 2008
Here you go:

  1. print reduce( lambda x,y: x+y, [int(i) for i in list(raw_input("Enter integer:"))] )
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,056
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 298
woooee woooee is offline Offline
Veteran Poster

Re: integer sum

 
0
  #5
Mar 11th, 2008
Without the lambda (which you probably haven't covered)
  1. input_str=raw_input("Enter a string of digits ")
  2.  
  3. ##-------------- Simple way --------------------------------------------------
  4. input_list = [int(x) for x in input_str]
  5. print sum(input_list), input_list
  6.  
  7. ##-------------- Check that it is a digit and sum manually -------------------
  8. input_list=[]
  9. errors=0
  10. for chr in input_str:
  11. if chr.isdigit():
  12. input_list.append(int(chr))
  13. else:
  14. print "Only the numbers 0 through 9 are allowed"
  15. errors=1
  16. if not errors:
  17. total=0
  18. for x in input_list:
  19. total += x
  20. print "total =", total, input_list
Last edited by woooee; Mar 11th, 2008 at 4:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 8
Reputation: bgeddy is an unknown quantity at this point 
Solved Threads: 1
bgeddy's Avatar
bgeddy bgeddy is offline Offline
Newbie Poster

Re: integer sum

 
0
  #6
Mar 12th, 2008
Originally Posted by rikxik View Post
Here you go:

  1. print reduce( lambda x,y: x+y, [int(i) for i in list(raw_input("Enter integer:"))] )
Nice bit of Python !!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 17
Reputation: rikxik is an unknown quantity at this point 
Solved Threads: 3
rikxik rikxik is offline Offline
Newbie Poster

Re: integer sum

 
0
  #7
Mar 12th, 2008
Originally Posted by bgeddy View Post
Nice bit of Python !!
Thanks. Actually, I realized that it should have been much more simpler:

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

Sometimes the obvious isn't obvious to me
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,297
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: integer sum

 
0
  #8
Mar 12th, 2008
Even simpler:
  1. sum([int(i) for i in raw_input("Enter integer: ")])
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 11
Reputation: Zelores is an unknown quantity at this point 
Solved Threads: 0
Zelores Zelores is offline Offline
Newbie Poster

Re: integer sum

 
0
  #9
Mar 12th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: integer sum

 
0
  #10
Mar 12th, 2008
Originally Posted by Zelores View Post
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.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC