I am having trouble with syntax


#1
  prices={"banana":4, "apple":2,"orange":15,"pear":3}
stock={"banana":6,"apple":0,"orange": 32,"pear":15}
for items in prices:
    print "prices: %s" % prices["banana","apple","orange","pear"]
    print "stock: %s" % stock["banana","apple","orange","pear"]

So I have no clue what the % %s do in this code, but they are in the example so a little explaining would be nice :frowning: . It said it needs three prints only have two, I don't know what to do for number three. What seems to be the problem here? :worried: :tired_face:


#2

When you wrote the for loop you phrased it as

for items in prices:

in other words you are telling the interpreter create an object(iterator that will have the same type as the keys in the dictionary i am looping through) that will go through each element in the dictionary. Therefore items represents every element in your dictionary meaning all you have to do is

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

#3

it says that a value for orange was wrong, which it was since 15 is supposed to be 1.5 but after i changed that i still get an error saying there is something wrong with orange


#4

in the prices dictionary prices is supposed to be 1.5 not 15


#5

i have the same issues as you.


#6

I copy-pasted your code still have me the orange error. Indention looked fine.


#7

I also have the orange error and I cannot see anything I did wrong?? It prints everything fine and I went back to the previous lesson to make sure that I had entered the correct numbers and they are?? No clue why it thinks the values for orange are wrong? help!!


#8

I give up for now. Gone around and around with this, now I'm tired and lost. Ideas people.

Write your code below!

def compute_bill(food):
total = 0
for key in food:
total += prices[key]
return total


#9

my code is

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

everything looks fine; the weird orange error everyone seems to be getting is preventing me form passing the lesson. Also, what do the %s and % mean in this context because I don't really know what these operators are doing?


#10

@bob1352 try this

for item1 in prices:
    for item2 in stock:
        print a
        print "price: %s"% prices[item1]
        print "stock: %s"% stock[item2]
        print

try using a nested for loop


#11

It worked,but can you please explain to me what each line of code does.