943,614 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 10158
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 7th, 2008
0

integer sum

Expand 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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Zelores is offline Offline
11 posts
since Mar 2008
Mar 7th, 2008
0

Re: integer sum

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.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Mar 11th, 2008
0

Re: integer sum

Click to Expand / Collapse  Quote originally posted by Zelores ...
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()
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sakthivelvm is offline Offline
1 posts
since Mar 2008
Mar 11th, 2008
0

Re: integer sum

Here you go:

Python Syntax (Toggle Plain Text)
  1. print reduce( lambda x,y: x+y, [int(i) for i in list(raw_input("Enter integer:"))] )
Reputation Points: 10
Solved Threads: 3
Newbie Poster
rikxik is offline Offline
17 posts
since Mar 2008
Mar 11th, 2008
0

Re: integer sum

Without the lambda (which you probably haven't covered)
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,302 posts
since Dec 2006
Mar 12th, 2008
0

Re: integer sum

Click to Expand / Collapse  Quote originally posted by rikxik ...
Here you go:

Python Syntax (Toggle Plain Text)
  1. print reduce( lambda x,y: x+y, [int(i) for i in list(raw_input("Enter integer:"))] )
Nice bit of Python !!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
bgeddy is offline Offline
8 posts
since Mar 2008
Mar 12th, 2008
0

Re: integer sum

Click to Expand / Collapse  Quote originally posted by bgeddy ...
Nice bit of Python !!
Thanks. Actually, I realized that it should have been much more simpler:

Python Syntax (Toggle Plain Text)
  1. sum([int(i) for i in list(raw_input("Enter integer:"))])

Sometimes the obvious isn't obvious to me
Reputation Points: 10
Solved Threads: 3
Newbie Poster
rikxik is offline Offline
17 posts
since Mar 2008
Mar 12th, 2008
0

Re: integer sum

Even simpler:
python Syntax (Toggle Plain Text)
  1. sum([int(i) for i in raw_input("Enter integer: ")])
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Mar 12th, 2008
0

Re: integer sum

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Zelores is offline Offline
11 posts
since Mar 2008
Mar 12th, 2008
0

Re: integer sum

Click to Expand / Collapse  Quote originally posted by Zelores ...
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.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: wx.ListCtrl Column 2 Editing
Next Thread in Python Forum Timeline: Tkinter quick question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC