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?
Zelores
0
Newbie Poster
Recommended Answers
Jump to PostYou 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 …
Jump to PostHi, 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 …
Jump to PostHere you go:
print reduce( lambda x,y: x+y, [int(i) for i in list(raw_input("Enter integer:"))] )
All 10 Replies
sneekula
969
Nearly a Posting Maven
sakthivelvm
0
Newbie Poster
rikxik
0
Newbie Poster
woooee
814
Nearly a Posting Maven
bgeddy
0
Newbie Poster
rikxik
0
Newbie Poster
sneekula
969
Nearly a Posting Maven
Zelores
0
Newbie Poster
bumsfeld
413
Nearly a Posting Virtuoso
Zelores
0
Newbie Poster
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.