| | |
integer sum
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 11
Reputation:
Solved Threads: 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?
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.
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.
•
•
Join Date: Mar 2008
Posts: 1
Reputation:
Solved Threads: 1
•
•
•
•
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()
•
•
Join Date: Mar 2008
Posts: 17
Reputation:
Solved Threads: 3
Here you go:
Python Syntax (Toggle Plain Text)
print reduce( lambda x,y: x+y, [int(i) for i in list(raw_input("Enter integer:"))] )
•
•
Join Date: Dec 2006
Posts: 1,001
Reputation:
Solved Threads: 284
Without the lambda (which you probably haven't covered)
Python Syntax (Toggle Plain Text)
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
Last edited by woooee; Mar 11th, 2008 at 4:07 pm.
•
•
•
•
Here you go:
Python Syntax (Toggle Plain Text)
print reduce( lambda x,y: x+y, [int(i) for i in list(raw_input("Enter integer:"))] )
•
•
Join Date: Mar 2008
Posts: 17
Reputation:
Solved Threads: 3
Thanks. Actually, I realized that it should have been much more simpler:
Sometimes the obvious isn't obvious to me
Python Syntax (Toggle Plain Text)
sum([int(i) for i in list(raw_input("Enter integer:"))])
Sometimes the obvious isn't obvious to me
Even simpler:
python Syntax (Toggle Plain Text)
sum([int(i) for i in raw_input("Enter integer: ")])
No one died when Clinton lied.
![]() |
Similar Threads
- big integer calculator (C++)
- what is C++ builder code for sum (C++)
- rolling dice (C)
- Using a for loop to sum an integer n and call function add_it (C)
- Sum of digits (C++)
- Please help me out, need help (C)
Other Threads in the Python Forum
- Previous Thread: wx.ListCtrl Column 2 Editing
- Next Thread: Tkinter quick question
| Thread Tools | Search this Thread |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui heads homework http ideas import input itunes java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite ssh statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






