# Something of Value (10 - 9) Confused with output

#1

Here is my code, my confusion lies with the output which results in four numbers: 48, 93, 117, 117

I cannot figure out what these numbers (aside from 117 which is the total) are, and why they are printed. Could someone help me understand?

prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}

total = 0

for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]

for key in prices:
total += stock[key]*prices[key]
print total

formatted code:

``````prices = {
"banana" : 4,
"apple"  : 2,
"orange" : 1.5,
"pear"   : 3,
}
stock = {
"banana" : 6,
"apple"  : 0,
"orange" : 32,
"pear"   : 15,
}

total = 0

for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]

for key in prices:
total += stock[key]*prices[key]
print total``````

#2

@moke,

• leave one blank-line above of your code
• select your code in the Post
• then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

With Courtesy to stetim94

#3

I believe your code format should be this to give the said output:

``````prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}

total = 0

for key in prices:
print(key)
print "price: %s" % prices[key]
print "stock: %s" % stock[key]

for key in prices:
total += stock[key]*prices[key]
print total``````

And you have this as the output:

``````orange
price: 1.5
stock: 32
pear
price: 3
stock: 15
banana
price: 4
stock: 6
apple
price: 2
stock: 0
48.0
93.0
117.0
117.0``````

Now, those numbers 48.0, 93.0, 117.0, 117.0 is a result of what you did here!

``````for key in prices:
total += stock[key]*prices[key]
print total``````

With your initial `total` value set to `0`. For the first instance, stock[key] * prices[key] would be 1.5*32 for orange to give 48, then {48 + (3*15)} will give 93....

Hope this helps!

#4

Thanks for the reply! So it performs the command for each key, and gives each iteration of the summing process, is that correct?

Why is orange the first key that's summed when it is the third piece of data inputted?

#5

yes, take a look:

``````for key in prices:
print key
total += stock[key]*prices[key]
print total``````

because the total is inside the loop, it will print the total each time. 48 is oranges (1.5 * 32), and so on. I also printed the key so you can see what product is being added.

If you put the print total outside the for loop (no indent) only the total is printed

Dictionary's are unordered. Well, that means: there is order, but not for you to determine.

#6

And is there no default ordering system in python? The only order I could see was sorting them by price ascending.

#7

There is order, list are ordered. Dictionary's are not. Why dictionary's are unordered? Because of the inner working of python, something with a hashtable. as you can read here

#8

Well said! List are ordered unlike dictionaries (which are ordered in some sort but not logical to user view kinda)

#9

I forgot, but could you please elaborate on the significance of the += in --------total += stock[key]*prices[key]-----------?

I know you can use it in sort of in place of .append() when you wan't to add something to a list, but does the function of it change when it's under a for loop, as it was here? And if so, how?

#10

it is short-cut for

total = total + stock[key]*prices[key