Keeping track of the produce problem


#1





kindly tell how can i reduce the code in three print statements.i have made these two options, but we have to use only three print statements only.So what am i missing.Kindly first give hint/approach so that i can figure it out on my own .


#2

the great thing about loops is that we can print each value in list:

for something in a_list:
     print something

we can even use the loop iterator as key to retrieve values from dictionary


#3

Do you even need the if statement or every for loop? Look at what you are doing on print("stock: %s"%stock[value]. What does it print?


#4

i could come with this, but still it is showing error.now kindly tell what is wrong with my approach. also it is printing values four times.


#5

this is what i could do with the little knowledge i have developed so far.


#6

why don't you use the loop iterator (value) as key to retrieve information about price and stock from the dictionaries?

when accessing a dictionary by key, the key can also be a variable containing a string (loop iterator for example)


#7

i used (value) with if statement, then also it does not change anything.same error.kindly explain what u meant sir.


#8

but why would you use if statements? You can just use value as key to retrieve information about stock and price directly:

prices[value]

if that is placed inside a loop, it will print the price of every product


#9

sir, i did that before, but could not get output like this.-

           banana apple orange pear

price 4 2 1.5 3
stock 6 0 32 15

the above code in attached file clears the exercise but the output of program is not what is expected. the values are not arranged according to fruit name(i.e they are arranged randomly).Also the values are printed four times .Kindly explain, what modification to be done to code so that output is proper and in systematic way and also does not repeat four times.


#10

but you don't want this, you want:

banana
4
6
apple
2
0

and so on.

well, value can also be used to print out the product name

The order is not important, why would it be important? Dictionaries are unordered by nature given they are accessed by key and not by index


#11

ok sir, got your point.thanks for the guidance.


#12

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