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 .
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
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?
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.
this is what i could do with the little knowledge i have developed so far.
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)
i used (value) with if statement, then also it does not change anything.same error.kindly explain what u meant sir.
but why would you use if statements? You can just use
value as key to retrieve information about stock and price directly:
if that is placed inside a loop, it will print the price of every product
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.
but you don't want this, you want:
banana 4 6 apple 2 0
and so on.
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
ok sir, got your point.thanks for the guidance.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.