allGuests = {'Alice': {'apples': 5, 'pretzels': '12'}, 'Bob': {'sendwiches': '3', 'apples': '12'}, 'Carol': {'cups': '3', 'apple pies': '1'}} def totalBrought(guests, item): numBrought = 0 for k, v in guests.items(): numBrought = numBrought + v.get(item, 0) return numBrought print('Number of things being brought: ') print(' - Apples ' + str(totalBrought(allGuests, 'apples'))) print(' - Cups ' + str(totalBrought(allGuests, 'cups'))) print(' - Sendwiches ' + str(totalBrought(allGuests, 'sendwiches'))) print(' - Pretzels ' + str(totalBrought(allGuests, 'pretzels'))) 

Conclusion:

 Traceback (most recent call last): File "C:/Users/eeNNdd11/PycharmProjects/course python/Что с собой принесли гости.py", line 17, in <module> print(' - Pretzels ' + str(totalBrought(allGuests, 'pretzels'))) File "C:/Users/eeNNdd11/PycharmProjects/course python/Что с собой принесли гости.py", line 9, in totalBrought numBrought = numBrought + v.get(item, 0) TypeError: unsupported operand type(s) for +: 'int' and 'str' 

Closed due to the fact that off-topic participants 0xdb , Enikeyschik , andreymal , Kromster , freim May 2 at 10:53 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reasons:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - 0xdb, Kromster, freim
  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Enikeyschik, andreymal
If the question can be reformulated according to the rules set out in the certificate , edit it .

    2 answers 2

    Learn to read the text of the error! It always states what the problem is.

    Here, for example:

     {'apples': 5, 'pretzels': '12'} 

    In this code, 5 is the number 5, and '12' not the number 12, but the text "12". And so you got messed up everywhere. And the numbers and the text can not be folded.

    • Fixed a mistake with the data types, but the output of the entire program is as follows: Number of things being brought: - Apples 5 - sups 0 - Sendwiches 0 - Pretzels 12 in the dictionary indicates that cups = 3, and the output, their 0, could not explain why ? - Vyacheslav Bondarev

    Your data types are different, you need this:

     allGuests = {'Alice': {'apples': 5, 'pretzels': 12}, 'Bob': {'sendwiches': 3, 'apples': 12}, 'Carol': {'cups': 3, 'apple pies': 1}} 
    • Fixed a mistake with the data types, but the output of the entire program is as follows: Number of things being brought: - Apples 5 - sups 0 - Sendwiches 0 - Pretzels 12 in the dictionary indicates that cups = 3, and the output, their 0, could not explain why ? - Vyacheslav Bondarev
    • @Vyacheslav Bondarev "return numBrought" is not in place. It should be on the same level with for, but like you, the iteration is performed only once for the first value of k, v. - Ghostik2005