Transcribing for-loop in one line to staircase for-loop

48 Views Asked by At

My task is to transcribe one line for-loop:

mystr = '12345'
result = sum(int(i) for i in mystr)
print(result)

into staircase for-loop with indentations.

The first one

result = sum(int(i) for i in mystr)

works just fine. This is what I tried to do and it didn't work. Can anyone help, please?

mystr = "12345"
for i in str:
    result = sum(int(i))
print(result)
1

There are 1 best solutions below

0
S.B On

try this:

mystr = '12345'
result = 0
for i in mystr:
    result += int(i)
print(result)

You have to have initial integer value because you're going to add integers together that's why I used result = 0.