1

I am trying to add the digits from a string and multiply it by 2.

ex) "123" would be (1+2+3)*2

I have tried this but I can't seem to get it working.

def add_digits_in_string(dig):
    initial = 0
    strinitial = str(initial)
    for i in dig:
        t = str(i)
        if t.isdigit():
            strinitial += int(t)
            strinitial*2
    return strinitial
4
  • strinitial*2 is not doing anything useful, and its indentation does not match any other indentation. Except now someone has edited your post and altered the indentation. Commented Oct 24, 2019 at 7:41
  • still doesnt work , the output for 12 was 0123 Commented Oct 24, 2019 at 7:42
  • @gm2213 i'm surprised that you're getting any output at all. strinitial += int(t) should throw a TypeError Commented Oct 24, 2019 at 7:50
  • You must name your method using snake_case If you've some kind of input control before that, I mean, if you trust the dig parameter, you can do it with one loc (my answer below). Commented Oct 24, 2019 at 8:07

6 Answers 6

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

You can use built-in function sum, list expansion and int to convert a digit to a string. You can iterate over consecutive characters easily.

In [8]: s = "123"

In [9]: 2*sum([int(ss) for ss in s])
Out[9]: 12

2 Comments

def adddigits(num): return 2*sum([int(i) for i in num]) , do you mean doing this? @gonczor
@gm2213 yes, if you want to have it in a function :-)
0

Try this using list comprehension:

num = '123'
print(sum(int(x) for x in num)*2)

and here you are if you really need that in a function:

def adddigitsinstring(dig):
    return sum(int(x) for x in dig)*2

num = '123'
print(adddigitsinstring(num))

Comments

0

You can sum a list comprehension the return the result multiplied by 2

def adddigitsinstring(dig):
    return sum([int(i) for i in dig if i.isdigit()]) * 2

print(adddigitsinstring("123"))

Comments

0

The most simple I could come up with:

def adddigitsinstring(dig):
  return 2*sum(map(lambda x:int(x), dig))

Sample tests:

>>> adddigitsinstring("1234")
20
>>> adddigitsinstring("9")
18

Comments

0

You don't need that strinitial variable, only an int where to store the partial sum:

def adddigitsinstring(dig):
    sum=0
    for i in dig:
        if i.isdigit():
             sum=sum + int(i)
    return sum * 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.