Python Forum
sum of number's caracters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sum of number's caracters
#6
To give you some ideas: there are simple ways to sum digits in number.

For strings ('give me character converted into int for every character in string and sum'):

>>> s = '123456'
>>> sum(int(char) for char in s)
21
For integer 'add reminder to total until there is no floor':

>>> num = 123456
>>> total = 0
>>> while num:
...     num, reminder = divmod(num, 10)
...     total += reminder
... 
>>> total
21
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
sum of number's caracters - by foli - Oct-28-2019, 09:44 AM
RE: sum of number's caracters - by buran - Oct-28-2019, 09:48 AM
RE: sum of number's caracters - by perfringo - Oct-28-2019, 09:48 AM
RE: sum of number's caracters - by foli - Oct-28-2019, 10:36 AM
RE: sum of number's caracters - by foli - Oct-28-2019, 11:53 AM
RE: sum of number's caracters - by perfringo - Oct-28-2019, 12:48 PM
RE: sum of number's caracters - by MckJohan - Nov-02-2019, 06:26 AM
RE: sum of number's caracters - by jefsummers - Nov-02-2019, 11:53 AM

Forum Jump:

User Panel Messages

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