Why does this actually work?


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

for key in prices:
    print key
    print "price: %s" % prices[key]
    print "stock: %s" % stock[key]
    
total = 0
for key in prices:
    print prices[key] * stock[key]
    total +=prices[key] * stock[key]
print total```

I don't get why this actually works. Why does prices[key] return a number rather than the string. E.g. why does it return "4" rather than "banana"?

#2

you learned that dictionary_name[key_name] gets you the value from dictionary, right? So key will contain the keys, which you use to retrieve values from dictionary


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.