Enter the description here. I need a function to calculate the amount of entries that are stored in the dictionary. The data type of the records is int . Dictionary example:

 stock = { "banana": 6, "apple": 0, "orange": 32, "pear": 15 } prices = { "banana": 4, "apple": 2, "orange": 1.5, "pear": 3 } 

this is an assignment from an online lesson, knocks out an error, as if following the example I tried to do but fails, I can not understand the reason for the error

  • Just do not understand what you want to know here. The given code is correct, the indicated error has nothing to do with it. Give exactly the code that caused this error. - Edward Izmalkov
  • @Eduard Izmalkov thank you for the answer, everything seems to be true, this is an online tutor asks me to turn on + PRO for money or else I won’t go to the next level - learning, I may not correctly interpret the task itself, conditions are a bit incomprehensible, task is in English and the translators are not thorough in terms of translation) I will copy and show if there is an opportunity please take a look! Here is the Task! at once I will throw off! - DancingMaster
  • 01. Define a function compute_bill that takes one argument food as input. 02. Create the variable with the initial value of zero. 03. For each item in the food list. 04. Finally, return the total. Ignore it. For food food list. - DancingMaster
  • Determine the function that compute_bill takes one argument in the food quality of the input data. 02. In the function, create the variable total with an initial value of zero. 03. For each item in the food list, add the price of this item to the total. 04. And finally, .returntotal Ignore the item or not, you are billing for in stock. Please note that your function should work for any food list. - DancingMaster
  • No, the code in the screenshot corresponds to the task and is correct. In error, a link to the wrong line compute_bill(['apple']) . The error code cannot cause this error. Therefore either you give not everything, or the interpreter invents something himself. - Edward Izmalkov

1 answer 1

All dictionary values ​​can be obtained using the values method, and the sum can be obtained using the sum function

 In [8]: stock.values ​​()
 Out [8]: dict_values ​​([0, 6, 32, 15])

 In [9]: sum (stock.values ​​())
 Out [9]: 53
  • understand @Alexander Zinin, the condition of the problem is such, create your own function in which there will be a for loop in which you need to turn to some lists, and for example, a sum, and if the condition is other tasks, that if it is a string type and I need to edit at my discretion !? Well, also from the int data type, for example, you need to apply a condition inside a function, such as comparing numbers or whatever action you take, it’s not at all what I'm looking for, sorry, but thanks for the advice! - DancingMaster