Help me, please!


#1

Hi everyone!
I was coding a simple program for school using the codes in the first part of the lesson "practice makes perfect".
I have Python 3.4.2 on my pc.When I'm writing this code, it's throwing an error:


def digit_sum(n):
            n=str(n)
            b=0
            for a in n:
                a=int(a)
                b+=a
            print('The sum of the digits is ',b)
        print(digit_sum(num))

Traceback (most recent call last):
  File "E:/folder/program", line 57, in <module>
    print(digit_sum(num))
  File "E:/folder/program.py", line 54, in digit_sum
    a=int(a)
ValueError: invalid literal for int() with base 10: '.'

I'm really stuck and I need to prepare it for tomorrow!So please tell me what to do.


#2

Make this variable a list and your code should work :slight_smile:


#3

Thank you.It is working now.I think the problem was with another part of the code.


#4

No that was the issue, hence the error,


#5

Earlier in this code, I had written:

num=float(raw_input("write your number."))

and so, num was a float.For example, if I enter 74, the float is 74.0 and when it would be converted to string, it would be "74.0".and when it goes throgh:

for a in n:
    a=int(a)

and ' . '(dot) can't be converted to int.
So I converted 'num' to int and the problem was solved.
Thank you for your help^^


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.