Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从字典中迭代值时获取类型错误_Python_Python 3.x - Fatal编程技术网

Python 从字典中迭代值时获取类型错误

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

我试图创建一个程序,返回一个位置上的董事会(从位置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: 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
    案例几乎没有任何作用。您可以只检查关键字是否在字典中
  • 编辑:看起来OP想要做的是
    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
    案例几乎没有任何作用。您可以只检查关键字是否在字典中
  • 编辑:看起来OP想要做的是
    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):
    :无需获取相同的