0
Understanding code
I donât understand the order of executions of a program or code. Can someone help me please understand d = [ ['T',25,18,'01'], ['X',25,18,'01'], ['F',25,18,'32'], ['X',25,18,'19'], ['X',25,18,'01'], ] L = len(d) c = 0 for i in range (0,L): r = d[i] if (r[0] == 'X' and r[3] == '01'): print (d[i]) c = c + 1 if (c >= 1): print(c) else: print('-')
4 Answers
+ 3
Link the code if you need help
+ 1
Most times the execution of a program is top to bottom, left to right
+ 1
d = [
['T',25,18,'01'],
['X',25,18,'01'],
['F',25,18,'32'],
['X',25,18,'19'],
['X',25,18,'01'],
]
L = len(d)
c = 0
for i in range (0,L):
r = d[i]
if (r[0] == 'X' and r[3] == '01'):
print (d[i])
c = c + 1
if (c >= 1):
print(c)
else:
print('-')
+ 1
Execution is top to bottom. When reading the code, try to find a verbal expression for what a segment of code does. Often times several lines of code belong logically together. If the code is well-written, it should be visible how the lines are logically grouped.
In your case, the first three instructions are assignments. A table, its length and some variable set initially to zero.
Then comes a loop. It ranges over the number of rows. So, we might say: "for each row in the table d ..." Let's look at the loop body. The loop body fetches the i-th row. If the first element is 'X' and the fourth '01', then print the row and increment c.
That means, once the loop is finished, c will hold the number of rows starting with 'X' and ending with '01'. And those rows have been printed.
Finally, if we counted at least one such row, print the total number of rows of that kind. Otherwise, print '-'.
The code's purpose is unknown to me, but that is how I read codes :) I hope it helped!