7 Replies - 227 Views - Last Post: 29 October 2013 - 02:50 PM Rate Topic: -----

#1 ProgramKid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-October 13

Take an integer and return the digits added together?

Posted 28 October 2013 - 11:19 AM

Totally lost on how to do this. Can someone please explain how to do this code? or give an example close to it?

Full Q:
You will write a function named countDigit that will take an integer and returns the digits added together. For instance, the number 123 will return 6 (1 + 2 + 3).
Is This A Good Question/Topic? 0
  • +

Replies To: Take an integer and return the digits added together?

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8365
  • View blog
  • Posts: 31,080
  • Joined: 12-June 08

Re: Take an integer and return the digits added together?

Posted 28 October 2013 - 11:27 AM

Convert the integer into a string.. a string is just an array of characters, right? Use that with a loop to visit each character in the array.. add'em together!
Was This Post Helpful? 0
  • +
  • -

#3 ProgramKid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-October 13

Re: Take an integer and return the digits added together?

Posted 29 October 2013 - 12:57 PM

help. can't figure it out. Trying to take an integer and returns the digits added together. For instance, the number 123 will return 6 (1 + 2 + 3).
but for some reason I keep getting like 212 for 12.

def countDigits():
    loop = str(input("Enter digits you want to be added:"))
    
    for ctr in loop:
        int(loop)
        ctr += loop
    print(ctr)

countDigits()


Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8365
  • View blog
  • Posts: 31,080
  • Joined: 12-June 08

Re: Take an integer and return the digits added together?

Posted 29 October 2013 - 01:01 PM

Please do not open duplicate topics.

Merging.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,100
  • Joined: 19-March 11

Re: Take an integer and return the digits added together?

Posted 29 October 2013 - 02:32 PM

View Postmodi123_1, on 28 October 2013 - 01:27 PM, said:

Convert the integer into a string.. a string is just an array of characters, right? Use that with a loop to visit each character in the array.. add'em together!



In most languages, this is actually not going to work. The character '1' is not the integer 1, so an additional step is required. I think it might work in PHP, but few languages are so type-irresponsible.


ProgramKid, you're actually pretty close. The problem you're having now is that the int() function doesn't change its argument, it returns a value. So contemplate this for a minute and see if it helps:

>>> foo = '123'
>>> int(foo)
123
>>> type (foo)
<type 'str'>
>>> bar = int(foo)
>>> type (bar)
<type 'int'>
>>> 

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8365
  • View blog
  • Posts: 31,080
  • Joined: 12-June 08

Re: Take an integer and return the digits added together?

Posted 29 October 2013 - 02:36 PM

Well yeah.. I figured type casting was sort of *hand waving* implied.. or something.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,100
  • Joined: 19-March 11

Re: Take an integer and return the digits added together?

Posted 29 October 2013 - 02:41 PM

I think that's a big assumption when we're dealing with a sum-the-integers problem... :)
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8365
  • View blog
  • Posts: 31,080
  • Joined: 12-June 08

Re: Take an integer and return the digits added together?

Posted 29 October 2013 - 02:50 PM

Tou-freaking-che'
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1