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 PostEven simpler:
sum([int(i) for i in raw_input("Enter integer: ")])
Jump to Postthanks 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 …
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.