Keeping Track of the Produce


#1


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

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

It should work because the output is correct

orange
price: 1.5
stock: 32

pear
price: 3
stock: 15

banana
price: 4
stock: 6

apple
price: 2
stock: 0

None


for fruit in prices:
    if fruit == 'banana':
            print "banana"
            print "price: %s" % prices['banana']
            print "stock: %s" % stock['banana']
            print
    if fruit == 'apple':
            print "apple"
            print "price: %s" % prices['apple']
            print "stock: %s" % stock['apple']
            print
    if fruit == 'orange':
            print "orange"
            print "price: %s" % prices['orange']
            print "stock: %s" % stock['orange']
            print
    if fruit == 'pear':
            print "pear"
            print "price: %s" % prices['pear']
            print "stock: %s" % stock['pear']
            print


#2

The instructions clearly state that you should only have three print statements... you have 16 print statements. That is more than 5 times the limit.


#3

How would you do that in three print statement? Can you give me an example?


#4

The instructions gave you an example, please note its syntax:

once = {'a': 1, 'b': 2}
twice = {'a': 2, 'b': 4}
for key in once:
    print "once: %s" % once[key]
    print "twice: %s" % twice[key]
  1. In the above example, we create two dictionaries, once and twice, that have the same keys.

  2. Because we know that they have the same keys, we can loop through one dictionary and print values from both once and twice.


#5

you need to print the name of the fruites (first print), price (second print), stock (the third print)


#6

If I go with your approach, I'll still have to use more than 3 print statements. At least 3 per fruit. Something like this:

for fruit in prices:
if fruit == 'banana':
print "banana"
print "price: %s" % prices['banana']
print "stock: %s" % stock['banana']


#7

I also tried one print statement per fruit but there are four fruits. This is what i got. It runs fine, but it's still telling throwing the same error.

for fruit in prices:
if fruit == 'banana':
print "banana \nprice %s \n" % prices['banana'], "stock %s" % stock['banana']
if fruit == 'apple':
print "apple \nprice %s \n" % prices['apple'], "stock %s" % stock['apple']
if fruit == 'orange':
print "orange \nprice %s \n" % prices['orange'], "stock %s" % stock['orange']
if fruit == 'pear':
print "pear \nprice %s \n" % prices['pear'], "stock %s" % stock['pear']


#8

I ment this:

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


#9

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]
this is still saying that the code is wrong for oranges. The order is different is that the reason it says my code is incorrect?


#10

This user did what you have suggested but still get the same error, see link below


#11

Well i kept getting the error and I came here to find out why
Oops, try again. Check what your code prints for orange. It doesn't look quite right! = This is the message i receive.


#12

prices = {"banana": 4, "apple": 2, "orange": 1.5, "pear": 3}
stock = {"banana": 6, "apple": 0, "orange": 32, "pear": 15}
for item in prices and stock:
print item
print "price: %s" % prices[item]
print "stock: %s" % stock[item]

This code worked for some reason putting prices and stock in the for loop let me pass the excerise, the console still shows the exact same thing, guess its a bug with the program


#13

I'm still getting the same error = "Oops, try again. Check what your code prints for orange. It doesn't look quite right!" even after I tried what you did. Anyone else have another workaround?


#14

All the codes I have posted on here do give me the expected output, but like you said I am starting to think there is a bug. I'm not sure how to use 3 print statements while there are 4 items. Hopefully someone can shed some light


#16

Solved

Problem founded:

This link has the answer well explained https://www.codecademy.com/forum_questions/548e642d86f552148f007ae3


#17

for key in prices :
** print key**
** print("Price: %s" %prices[key])**
** print("Stock: %s" %stock[key])**

Here's my code and the output is :

orange
Price: 1.5
Stock: 32
pear
Price: 3
Stock: 15
banana
Price: 4
Stock: 6
apple
Price: 2
Stock: 0
None

But i still get this error :
> Oops, try again. Check what your code prints for orange. It doesn't look quite right!


#18

Price: 1.5
Stock: 32

p and s should be lowercase.




Also don't post on other's topic.create your own :slight_smile:


#19

yeah, i figured it out in the next 10 secs i posted this thread, thanks though :slight_smile:


#20

Very good! Thanks for the help, I started losing my mind :smile:


#21

Thanks man, you were right, I was overthinking it.