Do I need to write another for loop to print and calculate the total?

Question

Do I need to write another for loop to print and calculate the total?

Answer

This step can actually be done either way! If you want to print and calculate the total inside of the loop you’ve already made, that works (assuming you do it correctly). If you want to make a new loop and keep it all separate, that works as well!

1 Like

2 posts were split to a new topic: Calculating Total Within a Loop

2 posts were split to a new topic: Loop Prints 117 as “Total”, But Not Getting Passed

Why does it print the items in the dictionary out of order? I’m really confused about that. It becomes difficult to keep tracking.

5 Likes

Is it possible to browse the lists of 02 different dictionaries when they have different keys?
For example :
text1 = {
‘1’ : ['The banana is ','The tomatoes are ',‘Too much’],
‘2’ : [‘a’,‘b’,‘c’]
}

text2 = {
‘a’ : [“Yellow”,“red”,“Rice”],
‘b’ : [1,2,3]
}

I would like to display :
The banana is Yellow, The tomatoes are Red, Too much Rice
a1, b2, c3

1 Like

There are lots of ways, but this one strikes me as simple and straight forward…

>>> text1 = {
'1' : ['The banana is','The tomatoes are','Too much'],
'2' : ['a','b','c']
}
>>> text2 = {
'a' : ["Yellow","Red","Rice"],
'b' : [1,2,3]
}
>>> paired_values = list(zip(text1['1'], text2['a']))
>>> for x in paired_values:
    print ("{object} {adjective}".format(object = x[0], adjective = x[1]))

	
The banana is Yellow
The tomatoes are Red
Too much Rice
>>> 
2 Likes

I have the same question.
the order is : banana apple orange pear
and the print is : orange pear banana apple
it’s not alphabetic order, price order or quantity order.
no sort in code.

what the matter ?

Python 2 dict objects are not ordered since they do not keep track of insertion order. There is nothing the matter. It’s how it is. Once you begin to work with Python 3 this is one thing you will see has changed (as of 3.6, if I’m not mistaken)

1 Like

It would be wonderful if they didn’t talk in riddles in the instructions. They way they say things make you think one thing is needed to be done then it turns out something else is needed to be done. Another for loop is a good example. Also, they didn’t give you freedom at the start now it’s like you’re sitting here wondering will you get it wrong if you add something you thought of.

10 Likes

text1 = {
‘1’ : [‘The banana is’,‘The tomatoes are’,‘Too much’],
‘2’ : [‘a’,‘b’,‘c’]
}
text2 = {
‘a’ : [“Yellow”,“Red”,“Rice”],
‘b’ : [1,2,3]}

for i in range(len(text1[‘1’])):
print(text1[‘1’][i]+’ '+text2[‘a’][i])
The banana is Yellow
The tomatoes are Red
Too much Rice

yes you do, try it again