8. Keeping Track, three print error


#1

Here's the code:

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

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

for fruit in prices:
    print(fruit)
    print("price: {}".format(prices[fruit]))
    print("stock: {}".format(stock[fruit]))

The keyword 'print' occurs a total of three times.
The error, as copied directly from the page:

Oops, try again. You should have exactly three print statements! (Make sure there are no occurrences of 'print' in any comment or string!)

It's not because of the print syntax; I checked (I still don't agree with CA using python 2, but that's irrelevant to this discussion). The output is correct. There's seemingly nothing wrong.

What gives?


#2

Try it with

print fruit
print "price: " + str(prices[fruit])
print "stock: " + str(stock[fruit])

#3

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

Create a stock dictionary with the values below.

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

for product in prices:
print product
print "price: %s" % prices[product]
print "stock: %s" % stock[product]

this is working


#4

this is also giving same error
Oops, try again. You should have exactly three print statements! (Make sure there are no occurrences of 'print' in any comment or string!)


#5

It wants the Python 2.7 syntax, so you can't have brackets with the print() function:

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

#6

Do you mean parentheses? The problem is that the code functions perfectly well with the syntax I used (and therefore must be valid syntax in whatever version CA is using), and the error message that pops up does not describe a problem that exists in my code.


#7

Turns out that it actually marks the same code as being correct if I add a space between the print and the open parenthesis:

print ('{stuff}'.format(stuff))

is marked as correct, while

print('{stuff}'.format(stuff))

is functionally the same but is marked as incorrect. It looks like whatever is checking the work does not correctly identify print() as being the same as print ().

Which way to file a bug report?

Also, great job to CodeAcademy for teaching against the recommendations of PEP 8 in part 9. Excellent work.


#8

Well, it worked for me. The error message says you don't have the right number of print functions, and that's probably because they're looking for 2.7-style print functions, and the 3-style ones don't register.