Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
summing digits of a number
#1
I've a problem with my code
get a number from user then summing digits of that number

num = input()
y = 0
for x in range(0, len(num)):

    y = int(num[x:x+1]) + int(num[x+1:x+2]) + y

print(y)
i cant finger out what's the problem
(my native language isn't English so may i have some mistakes)
Reply
#2
(Jun-29-2018, 06:37 AM)Megabeaz Wrote: I've a problem with my code
get a number from user then summing digits of that number

num = input()
y = 0
for x in range(0, len(num)):

    y = int(num[x:x+1]) + int(num[x+1:x+2]) + y

print(y)
i cant finger out what's the problem
(my native language isn't English so may i have some mistakes)


it was solved
i changed it to:
num = input()
if len(num) == 1:
    print(num)
else:
    while True:
        n_1 = int(num[:1])
        for x in range(1, len(num)):
            try:
                n_1 = int(num[x:x + 1]) + n_1

            except:
                n_1 = int(num[x:]) + n_1
        if len(str(n_1)) == 1:
            break
        else:
            num = str(n_1)
    print(n_1)
it's a program for summing digits of a number til
the number reach to one digit
Reply
#3
Now, that you have solved it - let me show you a Pythonic solution (works on 3.x only)

def sum_digits(number):
    digits_sum = 0
    while number:
        digit, *number = number
        digits_sum += int(digit)
    return digits_sum

PS Actually, while is less suitable than for in this case
def sum_digits(number):
    digits_sum = 0
    for digit in number:
        digits_sum += int(digit)
    return digits_sum
or in the shortest form, just
sum(map(int, number))
The latter, of course, takes time to get used to - unless you are a mathematician Tongue
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
The reason volano63's solutions are preferred is that it's better to loop directly over a string or list than to loop over the indexes of that string or list.

Another couple tips:

# These two ranges are the same
x = range(0, 6)
y = range(6)

# These two references are the same
a = s[x:x+1]
b = s[x]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Summing up set elements(HH:MM:SS) tester_V 4 1,176 Dec-22-2022, 10:03 AM
Last Post: perfringo
  Summing up rows and columns plumberpy 3 2,277 Aug-18-2021, 05:46 AM
Last Post: naughtyCat
Photo How can I use 2 digits format for all the number? plumberpy 6 2,348 Aug-09-2021, 02:16 PM
Last Post: plumberpy
  Digits of a number 1234 2 1,846 Nov-27-2020, 05:43 PM
Last Post: perfringo
  Single digits seem to be greater than double digits Frosty_GM 4 3,519 Nov-20-2020, 10:13 AM
Last Post: DeaD_EyE
  Grouping and summing of dataset jef 0 1,648 Oct-04-2020, 11:03 PM
Last Post: jef
  Please support regex for version number (digits and dots) from a string Tecuma 4 3,195 Aug-17-2020, 09:59 AM
Last Post: Tecuma
  Summing a list of numbers Oldman45 6 2,897 Jul-12-2020, 05:30 PM
Last Post: Oldman45
  Merge 2 dataframes but avoid double summing of rows? vinaysalian17 0 1,855 Jun-03-2020, 01:48 AM
Last Post: vinaysalian17
  filtering and summing data in a text file apexman 3 3,133 Mar-18-2019, 09:25 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020