嵌套字典循环与def(缩进问题)

huangapple go评论93阅读模式
英文:

Nested dictionary for loop with def (INDENTENTION PROBLEM)

问题

I am learning Python with the 2nd Edition of "Automate the Boring Stuff with Python," and I came across an indentation problem.

Can someone explain what I am doing wrong?

I am trying to add indentation after the for loop to insert the num_brought value, but I keep getting an error: TypeError: unsupported operand type(s) for +: 'int' and 'str'.

Book code:

  1. allGuests = {'Alice': {'apples': 5, 'pretzels': 12},
  2. 'Bob': {'ham sandwiches': 3, 'apples': 2},
  3. 'Carol': {'cups': 3, 'apple pies': 1}}
  4. def totalBrought(guests, item):
  5. numBrought = 0
  6. for k, v in guests.items():
  7. numBrought = numBrought + v.get(item, 0)
  8. return numBrought
  9. print('Number of things being brought:')
  10. print(' - Apples ' + str(totalBrought(allGuests, 'apples')))
  11. print(' - Cups ' + str(totalBrought(allGuests, 'cups')))
  12. print(' - Cakes ' + str(totalBrought(allGuests, 'cakes'))
  13. print(' - Ham Sandwiches ' + str(totalBrought(allGuests, 'ham sandwiches'))
  14. print(' - Apple Pies ' + str(totalBrought(allGuests, 'apple pies'))

My code:

  1. all_guests = {
  2. 'Alice': {'apple': 1, 'ham': 2},
  3. 'Joe': {'potato': 7, 'beer': 7},
  4. 'Carl': {'apple': 3, 'beer': 'none'}}
  5. def total_brought(guest, item):
  6. num_brought = 0
  7. for k, v in guest.items():
  8. num_brought = num_brought + v.get(item, 0)
  9. return num_brought
  10. print('Total brought: ')
  11. print('Apples: ' + str(total_brought(all_guests, 'apple')))
  12. print('Beer: ' + str(total_brought(all_guests, 'beer')))
  13. print('Ham: ' + str(total_brought(all_guests, 'ham')))
  14. print('Potato: ' + str(total_brought(all_guests, 'potato'))

(Note: I have corrected the indentation in your provided code to avoid the syntax error.)

英文:

I am learning Python with the 2°Ed. Automate the boring stuff with python, and I came across with a indentention problem.

Can someone explain what I am doing wrong?

I am trying to add a indentention after the for loop to insert the num_brought value, but it keeps getting an error: TypeError: unsupported operand type(s) for +: 'int' and 'str'.

Book code:

  1. allGuests = {'Alice': {'apples': 5, 'pretzels': 12},
  2. 'Bob': {'ham sandwiches': 3, 'apples': 2},
  3. 'Carol': {'cups': 3, 'apple pies': 1}}
  4. def totalBrought(guests, item):
  5. numBrought = 0
  6. for k, v in guests.items():
  7. numBrought = numBrought + v.get(item, 0)
  8. return numBrought
  9. print('Number of things being brought:')
  10. print(' - Apples ' + str(totalBrought(allGuests, 'apples')))
  11. print(' - Cups ' + str(totalBrought(allGuests, 'cups')))
  12. print(' - Cakes ' + str(totalBrought(allGuests, 'cakes')))
  13. print(' - Ham Sandwiches ' + str(totalBrought(allGuests, 'ham sandwiches')))
  14. print(' - Apple Pies ' + str(totalBrought(allGuests, 'apple pies')))

My code:

  1. all_guests = {
  2. 'Alice': {'apple': 1, 'ham': 2},
  3. 'Joe': {'potato': 7, 'beer': 7},
  4. 'Carl': {'apple': 3, 'beer': 'none'}}
  5. def total_brought(guest, item):
  6. num_brought = 0
  7. for k, v in guest.items():
  8. num_brought = num_brought + v.get(item, 0)
  9. return num_brought
  10. print('Total brought: ')
  11. print('Apples: ' + str(total_brought(all_guests, 'apple')))
  12. print('Beer: ' + str(total_brought(all_guests, 'beer')))
  13. print('Ham: ' + str(total_brought(all_guests, 'ham')))
  14. print('Potato: ' + str(total_brought(all_guests, 'potato')))

答案1

得分: 0

问题出在你的数据上,而不是缩进上。在这一行

  1. num_brought = num_brought + v.get(item, 0)

你将num_brought(一个数字)与allGuests中的一个字典的值相加。在书中,所有这些值也都是数字,但在你的数据中,其中一个是字符串'none'allGuests['Carl']['beer'])。

解决方法是将该数据更改为0,因为数字零表示什么都没有,或者"none":

  1. all_guests = {
  2. 'Alice': {'apple': 1, 'ham': 2},
  3. 'Joe': {'potato': 7, 'beer': 7},
  4. 'Carl': {'apple': 3, 'beer': 0}}
  5. def total_brought(guest, item):
  6. num_brought = 0
  7. for k, v in guest.items():
  8. num_brought = num_brought + v.get(item, 0)
  9. return num_brought
  10. print('Total brought:')
  11. print('Apples: ' + str(total_brought(all_guests, 'apple')))
  12. print('Beer: ' + str(total_brought(all_guests, 'beer')))
  13. print('Ham: ' + str(total_brought(all_guests, 'ham'))
  14. print('Potato: ' + str(total_brought(all_guests, 'potato'))

或者,你可以在函数中检查值是否为'none',并将其更改为零:

  1. all_guests = {
  2. 'Alice': {'apple': 1, 'ham': 2},
  3. 'Joe': {'potato': 7, 'beer': 7},
  4. 'Carl': {'apple': 3, 'beer': 'none'}}
  5. def total_brought(guest, item):
  6. num_brought = 0
  7. for k, v in guest.items():
  8. val = v.get(item, 0)
  9. if val == 'none':
  10. val = 0
  11. num_brought = num_brought + val
  12. return num_brought
  13. print('Total brought:')
  14. print('Apples: ' + str(total_brought(all_guests, 'apple')))
  15. print('Beer: ' + str(total_brought(all_guests, 'beer'))
  16. print('Ham: ' + str(total_brought(all_guests, 'ham'))
  17. print('Potato: ' + str(total_brought(all_guests, 'potato'))
英文:

The issue is your data, not your indentation. On this line

  1. num_brought = num_brought + v.get(item, 0)

you add num_brought (a number) to a value of one of the dictionaries in allGuests. In the book, all of those values are numbers too, but in your data, one of them is the string 'none' (allGuests['Carl']['beer']).

The solution is to just change that data to 0, as the number zero means having nothing, or "none":

  1. all_guests = {
  2. 'Alice': {'apple': 1, 'ham': 2},
  3. 'Joe': {'potato': 7, 'beer': 7},
  4. 'Carl': {'apple': 3, 'beer': 0}}
  5. def total_brought(guest, item):
  6. num_brought = 0
  7. for k, v in guest.items():
  8. num_brought = num_brought + v.get(item, 0)
  9. return num_brought
  10. print('Total brought: ')
  11. print('Apples: ' + str(total_brought(all_guests, 'apple')))
  12. print('Beer: ' + str(total_brought(all_guests, 'beer')))
  13. print('Ham: ' + str(total_brought(all_guests, 'ham')))
  14. print('Potato: ' + str(total_brought(all_guests, 'potato')))

Alternatively, you can check for the value 'none' and change it to zero in your function:

  1. all_guests = {
  2. 'Alice': {'apple': 1, 'ham': 2},
  3. 'Joe': {'potato': 7, 'beer': 7},
  4. 'Carl': {'apple': 3, 'beer': 'none'}}
  5. def total_brought(guest, item):
  6. num_brought = 0
  7. for k, v in guest.items():
  8. val = v.get(item, 0)
  9. if val == 'none':
  10. val = 0
  11. num_brought = num_brought + val
  12. return num_brought
  13. print('Total brought: ')
  14. print('Apples: ' + str(total_brought(all_guests, 'apple')))
  15. print('Beer: ' + str(total_brought(all_guests, 'beer')))
  16. print('Ham: ' + str(total_brought(all_guests, 'ham')))
  17. print('Potato: ' + str(total_brought(all_guests, 'potato')))

huangapple
  • 本文由 发表于 2023年3月12日 09:54:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定