For loop outputs too much


#1

So I’m doing the final exercise, Keeping Track Of Your Produce. I wrote a for loop that iterates over the one list and prints out info for the other. However, it prints out too much. The code is:



for Tesco in stock:
    print "banana" 
    print "price: %s" % prices[Tesco]
    print "%s" % stock[Tesco]
    print "apple"
    print "price: %s" % prices[Tesco]
    print "price: %s" % stock[Tesco]
    print "orange"
    print "price: %s" % prices[Tesco]
    print "%s" % stock[Tesco]
    print "pear"
    print "%s" % prices[Tesco]
    print "%s" % stock[Tesco] 


But for some reason, it outputs



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

As you can see, it’s outputting completely wring. Should I have another for loop or what? Why is it doing this? How can I fix it???


#2

the whole point of a loop is that we can print:

product name
price
stock

with just 3 print statements. to print the product, we can just print the loop iterator.


#3

But how do I use the for loop to print out the string part of the dictionaries?


#4

well, the string parts are stored in the loop iterator, so you can just print out the loop iterator.


#5

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