8/13 problem with the order that the fruits are printed


#1

for fruit in stock:
print fruit
print “Prices: %s” % (prices[fruit])
print “Stock: %s” % (stock[fruit])

The output is the following:
orange
Prices: 1.5
Stock: 32
pear
Prices: 3
Stock: 15
banana
Prices: 4
Stock: 6
apple
Prices: 2
Stock: 0

What am I doing wrong/why is the output in, like, numerical order? I keep getting CodeaAcademy saying “Check what your code prints for orange. It doesn’t look quite right!”


#2

Dictionaries are unordered by default, and may output in any order.

Check the example in the lesson text and don’t use uppercase in your labels.


#3

Thanks for noting that dictionaries are unordered by default. Since unordered generates an error message, I spent a lot of time trying to order the list. Anyway, I have a question about the Solution (below).

for food in prices:
print food----------------------This line is not in the example. Why is it necessary?
print “Price: %s” % prices[food]
print “Stock: %s” % stock[food]

Thank you


#4

This is a kind of label for the output, it is the name of the stock ittem for which the price and inventory are given.


#5

Thank you. Does this mean the example is incomplete and wouldn’t run correctly?

once = {‘a’ : 1, ‘b’: 2}

twice = {‘a’ : 2, ‘b’ : 4}

for key in once

print “Once: %s” % once[key]

print “Twice: %s” % twice[key]


#6

In neither instance above do we know the stock item for which the data is shown. That’s why we need a label. The key is the label, so we print it first.

I’d be careful where you go with the semantics in your interpretation. Stick to pragmatic terms that describe what they refer to. Needless, I get where you are coming from.


#7

Thank you.

(This line is to fulfill the following Codecademy email requirement: Codecademy will only accept an email if it contains twenty words or more.)


#8

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