Keeping Track of the Produce


#1

My code:

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

returns the correct output in the console but also the following error. "Oops, try again. You should have exactly three print statements! (Make sure there are no occurrences of 'print' in any comment or string!)"

The following for loop also outputs correctly in the console but the above error:

for key in prices:
    print(key)
    print("price: " + str(prices[key]))
    print("stock: " + str(stock[key]))

Any ideas? Seems like a bug.

SOLVED, UPDATE: Two posters below pointed out that changing print(key) to print key would allow code to pass lesson, which did indeed result.


#9

This is unfortunately really stupid. This has been an issue for many years now and there's been no word from Codecademy about this lesson. My solution only has 3 print statements... runs through all the Python interpreters I have tried without error. Yet it still is giving the same cryptic error which is an issue on the side of Codecademy. They have mostly had useful errors. This one is absurd.

Here is my code as proof. You can copy and paste this in and it won't work to pass, but the output is as it should be.

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('price: %s' % prices[key])
    print('stock: %s' % stock[key])

So far no one's "solution" has accurately identified the problem. Only what has worked for them. I want to know why this isn't working. I don't care if another solution that does the same thing as mine works if I don't know WHY mine doesn't.

Any ideas?

Edit: So there is NOTHING wrong with my code. However, Codecademy for this lesson ONLY requires that your print statements NOT include the parenthesis. I do realize this is not a Python 3 course, however print('something') and print 'something' are both correct ways in Python 2.7. I have been using the print() syntax for ALL of the other lessons. I'm attempting to be mostly Python 3 compatible even though for my application I am starting with Python 2.7 due to library restrictions at the moment.

Edit 2: So on the very next lesson I decided to convert it back to my previous (above) solution before adding new code. Guess what it was just fine with it. Why is that particular lesson for 2+ years been problematic? So stupid.

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("price: %s" % prices[key])
    print("stock: %s" % stock[key])
    
total = 0

for key in prices:
    total += prices[key] * stock[key]
    
print(total)

Thanks!
Jarvis


#11

Thank you, changing print(key) to print key allowed my code to pass the lesson.
It's a shame Code Academy hasn't fixed it yet if it is indeed an old bug. Other than this I have found the course to be very good!