Thursday, 15 August 2013

for statement (looping calculation priority)*not sure if this is the correct term

for statement (looping calculation priority)*not sure if this is the
correct term

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}
total = 0
for item in prices:
a=prices[item]*stock[item]
total=total+a
print total
so here is my code, i intentionally wrote the print total within the for
statement, so it print out the order of calculation for me and the number
goes like this 48.0 93.0 117.0 117.0, so after the basic calculation the
first value that the statement churns out is the total of orange the
second is the total of pear the third is the banana and the last is the
apple
now i am curious under what principle is this precedence of calculation
carried out cause it seems to me that it started with the largest number
in dic_stock and calculated in a descending manner, i do aware this is
just a superficial observation,there must be a reason why computer did
this
can anyone tell me why :)

No comments:

Post a Comment