My purpose:
"Write a function sumOfDigits, which has one parameter of the string type. The function should return the sum of the digits in the string. Do not treat multiple digital strings as one number -" 2014 "should be treated as 4 different digits from 2, 0, 1, 4. The function should return 17 for the string "Current Date 09/01/2014." You can assume that the parameter is a string. There is no need to perform type checking. "
Here is what I still have (with corresponding indentation):
def sumOfDigits (string1: str): summation=0 for i in string1: summation=summation + int (i) return (summation) print (sumOfDigits ('543tf'))
I get the following error:
"Traceback (most recent call last): File "C:\Users\Andrew\Desktop\lab3.py", line 45, in <module> print (sumOfDigits ('543tf')) File "C:\Users\Andrew\Desktop\lab3.py", line 42, in sumOfDigits summation=summation + int (i) ValueError: invalid literal for int() with base 10: 't'"
How can i solve this? Does it because of the difficulties associated with adding int and line / char?
source share