A day at the super market: Keeping Track of the Produce


#1


https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/1/3?curriculum_id=4f89dab3d788890003000096


The for statement starts at position 1 and skips by 2. The for statement should start at 1 and increase by 1 each iteration, however if you look at the output it indeed starts at position 1 (orange), but then skips by 2 to go to pear next.


I expect the for loop to iterate through each key properly, starting with banana. The output is:
Orange
Prices: 1.5
Stock: 32
pear
Prices: 3
Stock: 15
banana
Prices: 4
Stock: 6
apple
Prices: 2
Stock: 0
None


prices = {"banana": 4, "Orange": 1.5, "apple": 2, "pear": 3
    }
    
stock = {"banana": 6, "Orange": 32, "apple": 0, "pear": 15
    }
    
  
for i in prices:
    print i
    print "Prices: %s" %prices[i]
    print "Stock: %s" %stock[i]


#2

Should print,

Price: 1.5

#3

Thank you for the typo correction, however since Prices is in quotes it does not affect the output other than displaying Prices in the output. I could write:

print "Anyword: %s" %prices[i]

And it would pass at the end. The syntax error states that Orange is not where it should be, this is not the same as how a string displays within quotes.

It starts at position 2, and increments by 2. It has nothing to do with what you said. Can anyone else help?


#4

The point is, this is not what is expected. Check the example at left of the editor. Price: .... That is what the lesson checker is looking for.

It is not a syntax error, that would display in the console. This is an SCT error. There is a limited number of error messages so it just printed what it had.


#5

Why? Just to have you throw their advice back in their faces? You didn't even try what was suggested. All you are doing is digging yourself a deeper hole. I'll make a point of avoiding your questions in future.


#6

'''prices = {"banana": 4, "Orange": 1.5, "apple": 2, "pear": 3
}

stock = {"banana": 6, "Orange": 32, "apple": 0, "pear": 15
}

for i in prices:
print i
print "Price: %s" %prices[i]
print "Stock: %s" %stock[i]
'''

output
Orange
Price: 1.5
Stock: 32
pear
Price: 3
Stock: 15
banana
Price: 4
Stock: 6
apple
Price: 2
Stock: 0
None
=======
At Codeace, I did try his correction out before I posted it. Any reasonable person asking for help should, I wouldn't post again if it worked.
I didn't throw anything back, simply stated the facts as to why it wouldn't affect the output. It is ok to exchange information BOTH ways when troubleshooting.


#7

Are you saying it still doesn't pass?


#8

MTF, yes. I program Python a lot on the side and do this for fun at work so I don't understand why this is causing an issue. I don't understand why its not working. :frowning: What would cause it to start with index key 2, rather than 1 and skip by 2?


#9

Maybe that message about Orange isn't so far off... Try writing orange.

Dictionaries and not ordered. We cannot predict in which order they will be iterated.


#10

MTF, I found the problem. In the instruction assignment statement it says "you can access the stock dictionary while you are looping through prices." That is a different statement than saying "you MUST use the stock dictionary in the for loop." The problem was that the for loop was iterating through the Prices dictionary and not Stock.

Once I swapped those around it worked. Thank you for troubleshooting it with me.


#11

Mine still isn't working, read through all these comments to try and see if it was something you pointed out as the problem, but still can't seem to find the problem... Sorry


#12

It's just a bug in the codeacademy
You have to write
print "price: %s" % prices[key]
print "stock: %s" % prices[key]
Your string should be a lowercase text
Silly, I know :smiley:


#13

Oh that's not to good... Thank you that's a huge help!! :slight_smile:


#14

    print "stock: %s" % stock[key]

#15

Yeah, I know
Haven't noticed that while writing, sorry :smiley:


#16

thank you ! I was getting the same error. What a silly mistake by codecademy.


#17

how correct it?
i get the same error.. :frowning:


#18

#19

This topic is old, if you still have problems make a new topic and use the template to make a good topic, so someone can help you. Make sure your output is an exact match as the output show in the instructions