+ 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
Nguyen Hoang Ha
Nguyen Hoang Ha - avatar
4 Antworten
+ 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
Nguyen Hoang Ha
Nguyen Hoang Ha - avatar