Answer the question
In order to leave comments, you need to log in
Python for loop?
Please tell me how the values are added in the code below:
def print_shopping_list(pizza, salad):
dish = set (pizza)
dish = dish.union(salad)
for i in dish:
weight = 0
if i in pizza.keys(): weight += pizza[i]
if i in salad.keys(): weight += salad[i]
print (i,': ', weight, sep='')
pizza = {'мука, кг': 1,
'помидоры, кг': 1.5,
'шампиньоны, кг': 1.5,
'сыр, кг': 0.8,
'оливковое масло, л': 0.1,
'дрожжи, г': 50}
salad = {'огурцы, кг': 1,
'перцы, кг': 1,
'помидоры, кг': 1.5,
'оливковое масло, л': 0.1,
'листья салата, кг': 0.4}
Answer the question
In order to leave comments, you need to log in
Some strange question. "Adding" occurs according to the laws of arithmetic. The for loop is executed as many times as you have ingredients in the dish.
Zeroing the Weight "weight" - is clearly located not where it belongs - but its place is BEFORE the for operator. The for loop is executed as many times as you have ingredients in the dish.
For each ingredient, the weight of the added product is added to the current weight value, and for the pizza or salad dish that you specified.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question