NurlanK
NurlanK

Reputation: 11

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

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)

Upvotes: 1

Views: 45

Answers (1)

S.B
S.B

Reputation: 16564

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.

Upvotes: 1

Related Questions