Value is not taken from the list


#1


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

Traceback (most recent call last):
File "python", line 4, in
ValueError: 'orange' is not in list


value orange should be taken from the list and printed in output


prices = {"banana": 4, "apple": 2, "orange": 1.5, "pear": 3}
stock = {"banana": 6, "apple": 0, "orange": 32, "pear": 15}
for value in prices:    
    print prices.keys()[prices.values().index(value)]
    print "price %s" % prices[value]
    print "stock %s" % stock[value]


#2

You're just gonna have to rethink that.
I read that code and it just doesn't make sense, so I'd have you do the same.

If you have more specific questions then go right ahead, but my response to that is that you should just read it.


#3

I tried with different logic and it worked. anyways thanks for ur reply


#4

You might look at what prices.values() returns, since that is where you are looking for your orange
And if you have long expressions that aren't doing what they should then you may want to just split it up into smaller parts and look at their results.
But yeah, whatever you were doing, another approach would be better.


#5

@nareshiyer,
Changed your code

prices = {"banana": 4, "apple": 2, "orange": 1.5, "pear": 3}
stock = {"banana": 6, "apple": 0, "orange": 32, "pear": 15}
for value in prices:    
    print prices.keys()
    print prices.values()
    #print stock.index(value) 'dict' object has no 'index'
    print "price %s" % prices[value]
    print "stock %s" % stock[value]

#6

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