Python 从字典中迭代值时获取类型错误
我试图创建一个程序,返回一个位置上的董事会(从位置0开始)为每一个骰子卷 我的字典里有键,它们是棋盘上的位置 和值,它们是 应该发生的位移Python 从字典中迭代值时获取类型错误,python,python-3.x,Python,Python 3.x,我试图创建一个程序,返回一个位置上的董事会(从位置0开始)为每一个骰子卷 我的字典里有键,它们是棋盘上的位置 和值,它们是 应该发生的位移 dictionary = {64: -4, 49: -38, 98: -20, 16: -10, 87: -63, 56: -3, 47: -21, 93: -20, 62: -43, 95: -20, 80: 20, 1: 37, 51: 16, 4: 10, 21: 21, 71: 20, 9: 22, 28: 56, 36
dictionary = {64: -4, 49: -38, 98: -20, 16: -10, 87: -63, 56: -3, 47: -21, 93: -20, 62: -43,
95: -20, 80: 20, 1: 37, 51: 16, 4: 10, 21: 21, 71: 20, 9: 22, 28: 56, 36: 8}
例如,当我降落在位置64时,我在字典中找到键64,从64开始,我在棋盘上移动-4,停留在60上
我的当前代码如下
def location(rolls):
dictionary = {64: -4, 49: -38, 98: -20, 16: -10, 87: -63, 56: -3, 47: -21, 93: -20, 62: -43,
95: -20, 80: 20, 1: 37, 51: 16, 4: 10, 21: 21, 71: 20, 9: 22, 28: 56, 36: 8}
position = 0
list_position = []
for roll in rolls:
position = position + roll
if position not in dictionary:
pass
if position in dictionary:
position = position + dictionary.get(roll)
list_position.append(position)
print(position)
我得到了部分位置,我相信一些迭代会返回类型错误
>>> location([1, 4, 5])
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 12, in location
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
38
42
# desired output
[38, 42, 26]
>位置([1,4,5])
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第12行,在位置中
TypeError:不支持+:'int'和'NoneType'的操作数类型
38
42
#期望输出
[38, 42, 26]
从第一轮骰子开始,我们降落在位置1。我们在我的字典里找到了1,得知我
必须移动37,停留在38。从第二轮骰子开始,我们从38移动4,然后落地
42号。42不在我的字典里,所以我们就依靠42。然后从第三轮骰子开始,我们移动5
从42号降落到47号。47在我的字典里,我从47移动-21,在26降落
所以我实际上得到了列表中的前两个位置。我一点也不知道为什么会发生第三次
未打印位置并返回此类型错误。问题是
position=position+dictionary.get(roll)
。在roll=5的第三次迭代中,dictionary.get(roll)的计算结果为无
>>> location([1, 4, 5])
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 12, in location
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
38
42
# desired output
[38, 42, 26]
编辑:另外,如果要跳到for循环的下一个迭代,正确的语法是continue,而不是pass
我在下面添加了一个检查:
def location(rolls):
dictionary = {64: -4, 49: -38, 98: -20, 16: -10, 87: -63, 56: -3, 47: -21, 93: -20, 62: -43,
95: -20, 80: 20, 1: 37, 51: 16, 4: 10, 21: 21, 71: 20, 9: 22, 28: 56, 36: 8}
position = 0
list_position = []
for roll in rolls:
position = position + roll
if position not in dictionary:
continue # pass does nothing here
if position in dictionary:
if dictionary.get(roll):
position = position + dictionary.get(roll)
list_position.append(position)
print(position)
location([1, 4, 5])
问题是
position=position+dictionary.get(roll)
。在roll=5的第三次迭代中,dictionary.get(roll)的计算结果为无
编辑:另外,如果要跳到for循环的下一个迭代,正确的语法是continue,而不是pass
我在下面添加了一个检查:
def location(rolls):
dictionary = {64: -4, 49: -38, 98: -20, 16: -10, 87: -63, 56: -3, 47: -21, 93: -20, 62: -43,
95: -20, 80: 20, 1: 37, 51: 16, 4: 10, 21: 21, 71: 20, 9: 22, 28: 56, 36: 8}
position = 0
list_position = []
for roll in rolls:
position = position + roll
if position not in dictionary:
continue # pass does nothing here
if position in dictionary:
if dictionary.get(roll):
position = position + dictionary.get(roll)
list_position.append(position)
print(position)
location([1, 4, 5])
list\u position
和position\u list
应该是相同的列表5
不在字典中(因为您没有检查roll
,您只检查位置
),所以它返回None
,您肯定不能将None
添加到整数中。为不存在的键添加默认值李>
pass
的if
案例几乎没有任何作用。您可以只检查关键字是否在字典中dictionary.get(position)
。在这种情况下,我们不需要检查任何东西
我的意思是:
def location(rolls):
dictionary = {64: -4, 49: -38, 98: -20, 16: -10, 87: -63, 56: -3, 47: -21, 93: -20, 62: -43,
95: -20, 80: 20, 1: 37, 51: 16, 4: 10, 21: 21, 71: 20, 9: 22, 28: 56, 36: 8}
position = 0
list_position = []
for roll in rolls:
position = position + roll
# if position not in dictionary: # commented out
# pass #
# if position in dictionary: # no need to check anything since default is 0
position = position + dictionary.get(position, 0)
list_position.append(position) # fixed the name
print(position)
运行它:
location([1, 4, 5])
输出:
38
42
26
list\u position
和position\u list
应该是相同的列表5
不在字典中(因为您没有检查roll
,您只检查位置
),所以它返回None
,您肯定不能将None
添加到整数中。为不存在的键添加默认值李>
pass
的if
案例几乎没有任何作用。您可以只检查关键字是否在字典中dictionary.get(position)
。在这种情况下,我们不需要检查任何东西
我的意思是:
def location(rolls):
dictionary = {64: -4, 49: -38, 98: -20, 16: -10, 87: -63, 56: -3, 47: -21, 93: -20, 62: -43,
95: -20, 80: 20, 1: 37, 51: 16, 4: 10, 21: 21, 71: 20, 9: 22, 28: 56, 36: 8}
position = 0
list_position = []
for roll in rolls:
position = position + roll
# if position not in dictionary: # commented out
# pass #
# if position in dictionary: # no need to check anything since default is 0
position = position + dictionary.get(position, 0)
list_position.append(position) # fixed the name
print(position)
运行它:
location([1, 4, 5])
输出:
38
42
26
您的代码存在一些结构化问题:
if
块,而不是两个单独的块:
if position not in dictionary:
pass
else: # <-- use else instead
position = position + dictionary.get(roll)
list_position.append(position)
print(position)
字典.get(roll)
不受条件控制。您可以检查字典中的位置
,但不能确保滚动
也在字典中。因此,当它不在那里时,dict.get()
默认返回None
您可以设置一个默认值,如
字典。获取(roll,0)
,或者用您想要定义的所有可能的roll填充您的字典,或者检查字典中的roll
,您的代码有一些结构问题:
if
块,而不是两个单独的块:
if position not in dictionary:
pass
else: # <-- use else instead
position = position + dictionary.get(roll)
list_position.append(position)
print(position)
字典.get(roll)
不受条件控制。您可以检查字典中的位置
,但不能确保滚动
也在字典中。因此,当它不在那里时,dict.get()
默认返回None
您可以设置一个默认值,如
字典。获取(滚动,0)
,或将所有可能要定义的滚动填入字典,或检查滚动字典
请修复缩进。无法查看位置
的意图,因为5
不在dict中,所以字典中没有。get(roll)
返回无
,无法将其添加到integer@Ayxan“我已经修复了凹痕。”克里斯·兰兹,哦,我明白了。4和1在dict中,但5不是。我还没有用pass语句清除这个案例吗?请修正缩进。无法查看位置
的意图,因为5
不在dict中,所以字典中没有。get(roll)
返回无
,无法将其添加到integer@Ayxan“我已经修复了凹痕。”克里斯·兰兹,哦,我明白了。4和1在dict中,但5不是。我还没有用pass语句清除这个例子吗?只需使用position=position+dictionary.get(roll,0)
并删除额外的if dictionary.get(roll):
:不必两次提取同一个键。只需使用position=position+dictionary.get(roll,0)
并删除额外的if dictionary.get(roll):
:无需获取相同的