your code
def adddigitsinstring(dig): # dig is string "123"
initial = 0 # initial is of type int
strinitial = str(initial) # converting initial to string type into new varibale strinitial
for i in dig: # looping through each digit in the dig
t = str(i) # converting existing string to string , not required
if t.isdigit(): # check if digit char is digit
strinitial += int(t) # adding the int type with the str type wrong "2 +"2" == error
strinitial*2 # multiple value with the 2, so in every loop it got multiple each time , not required
return strinitial # return int value as str type ie "123"
this is what you should do, you need to keep checking the if each character in the input is a digit or not. if it is a digit then you need to keep adding the digits to a intial value of type int beacuse 1+1=2 and '1'+'1' ='11', once you add all the digits then multiply them by 2 and return the result in str format
def adddigitsinstring(dig:str):
intial = 0
for digit in dig:
if digit.isdigit():
intial += int(digit)
final_result = intial * 2
return str(final_result)
def add_digits_in_string(dig:str):
digit = [int(digi) for digi in list(dig) if digi.isdigit()]
return str(sum(digit) * 2)
print(adddigitsinstring("12345"))
print(add_digits_in_string("12345"))
output
30
30
strinitial*2is not doing anything useful, and its indentation does not match any other indentation. Except now someone has edited your post and altered the indentation.strinitial += int(t)should throw a TypeErrordigparameter, you can do it with one loc (my answer below).