Can someone please explain this in detail for me? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can someone please explain this in detail for me?

I just can't understand how this code works. addition_str = '2+5+10+20' s = 0 a = " " for i in range(len(addition_str)): if addition_str[i] <= "9" and addition_str[i] >= "0": a += addition_str[i] if addition_str[i] > "9" or addition_str[i] < "0" or i == len(addition_str) - 1: s += int(a) a = " " print(s)

7th Sep 2022, 11:59 AM
Sara Silvers
Sara Silvers - avatar
3 Answers
+ 2
It first reading digits into 'a' until a non-digit encounters. If on non-digit character, or at end, then 'a' is converted to int and added to 's' a is reset to "" Empty. So it's like s = 2 s = s + 5 = 7 s = s + 10 = 17 s = s + 20 = 37 Hope it helps...
7th Sep 2022, 12:15 PM
Jayakrishna 🇮🇳
+ 1
Visualize your code execution (Python, Java, C, C++, JavaScript, Ruby) https://pythontutor.com
7th Sep 2022, 12:12 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
I finally get it now. Thanks everyone for replying and explaining.
7th Sep 2022, 12:37 PM
Sara Silvers
Sara Silvers - avatar