`# Function to calculate the sum of digits in a number
def add_sum(a):
return sum([int(i) for i in str(a)])
Call the function with the number
result = add_sum(541646)
print(result)`your text``
I was trying to write a function for addition of numbers converting from string to integers colab throwing me value error
ValueError Traceback (most recent call last)
in <cell line: 6>()
4
5 # Call the function with the number
—-> 6 result = add_sum(541646)
7
8 # Print the result
2 frames
in (.0)
1 def sum(a):
—-> 2 return sum([int(i) for i in str(a)])
ValueError: invalid literal for int() with base 10: ‘[‘
Anish Melvin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.