+ 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)
3 Respostas
+ 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...
+ 1
Visualize your code execution
(Python, Java, C, C++, JavaScript, Ruby)
https://pythontutor.com
0
I finally get it now. Thanks everyone for replying and explaining.