8/13


#1

for printing the food the code I used was


for fruit in prices
  print fruit
  print prices[fruit]
  print stock[fruit]

this made the code come out looking exactly the same as the answer,


  print food
  print "price: %s" % prices[food]
  print "stock: %s" % stock[food]

so my question is what the heck does the “price: %s” % part of both of those do and why is it necessary?


#2

price: %s means we have a label in front of the product price. So the user knows its looking at the price, otherwise its just a number, what is the user suppose to know what it means?

if you get:

Apple
5
3

how do you know what 5 and 3 are? On the hand, if you use price: and stock: you get:

Apple
price: 5
stock: 3

now you know what is what


#3

thank you for your reply, it makes a lot more sense.


#4

Wait but then what does the % do that comes after the label, I thought that was the remainder sign for logic equations and such, what is it’s purpose in this?


#5

not in this case, %s a string placeholder, then after the string you place a % to tell python you are going to give the promised variables

% has a different function here


#6

My question ist: Why is orange printed first? From the order in the dictionary, I would expect the following order: banana, apple, orange, pear. However, it is printed: orange, pear, banana, apple. Why this odd order?


#7

cause dictionaries are unordered by nature, the order isn’t preserved. Given values are accessed by key and not by index.

Too truly understand this, you would have to understand the underlying hastable implementation. I wouldn’t bother with that just yet


#8

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