Keeping track of produce


#1



Not sure what's going on here. Just says check what your code is printing for orange but it looks correct to me.


prices = {
    "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 "prices: %s" % prices[key]
    print "stock: %s" % stock[key]


#2

Could you link the exercise? It'll make it much quicker to help. Thanks :slight_smile:


#3

Sure thing.


#4

The code checks out to me. Have you tried refreshing the browser and/or restarting the exercise?

I also noticed while doing the sass course that if you copied blocks from the instructions because you were too lazy to type @include for the 15th time, it tends to not work until you retype it.


#5

I lied, it appeared that in changing all of your "prices" to "price", it worked.


#6

Thanks friend. Kind of annoying that it wouldn't work with prices thought.


#7

Is there a reason that switching all the "prices" to "price" makes the code work? I'm new to coding and am not a fan of the finicky nature of it.


#8

As far as I can tell, no. I can't recall whether it was codecademy looking for specifics or if python gave the error, but either way I don't know.


#9

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
stocks = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

for key in prices:
if key == 'apple':
print key
print "price: %s" % prices[key]
print "stock: %s" % stocks[key]

why this piece of code not working???


#10

Had the same issue - changed it to "price" worked. Going to assume it's a codeacademy bug, can't see why it would matter....


#11

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

stock = {
    "banana": 6, 
    "apple": 0,
    "orange": 32,
    "pear": 15
}
for key in prices:
    if key == "banana":
        print key
        print "prices: %s" % prices[key]
        print "stock: %s" % stock[key]
for key in prices:
    if key == "apple":
        print key
        print "prices: %s" % prices[key]
        print "stock: %s" % stock[key]
for key in prices:
    if key == "orange":
        print key
        print "prices: %s" % prices[key]
        print "stock: %s" % stock[key]
for key in prices:
    if key == "pear":
        print key
        print "prices: %s" % prices[key]
        print "stock: %s" % stock[key]

Please why is this not working


#12

Hi @osegun24 ,

You have done more work than necessary. Only one for loop is needed here.

Tests, such as ...

if key == "banana":

... are unnecessary for this set of exercises.

Also, carefully compare the output from this statement ....

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

... to that specified in the instructions. Your output must match the specifications exactly.


#13

prices = {
    "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 "prices: %s" % prices[key]
    print "stock: %s" % stock[key]

i tried this initially before writing the code posted initially but there is still an error

Oops, try again. Check what your code prints for orange. It doesn't look quite right!`

"my error message"


#15

Here's the format specified in the instructions ...

apple
price: 2
stock: 0

Compare your output for apple to the above.


#16

Python is very naggy about indentations and proper casing; therefore, reminder and Reminder will be two different things. And 'test' is not the same as 'Test'.

Hope that helps.


#17

Hi there, at first let me appologize for my english. Now to your Problem, the Price of orange is 1.5 so its a float variable, as it looks like the code "print "prices: %s" % prices[key]" cant handle float variables, but thats just the Problem, I haven't found a solution to it yet.


#18

Concerning names of variables, you are absolutely correct that inconsistent use of spelling and case can cause a Python program to crash. On the other hand, concerning strings, you can output incorrect spellings and case if you want, as far as the Python interpreter is concerned. However, in the current exercise, Python will be happy to output a string containing "prices" instead of "price", but Codecademy will not accept such a departure from the instructions. The Python interpreter was happy with @osegun24's most recent code, but Codecademy was not.


#19

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