首页 > 解决方案 > Python dict 项目匹配返回 TypeError: 'int' object is not iterable

问题描述

我有以下代码,我想匹配用户输入的 x 和 y 值相乘结果以匹配字典项。例如用户输入 x=2 和 y=3,乘法结果将是 6。x 和 y 值的顺序很重要,它应该与字典顺序匹配。例如 x=3 和 y=2 并不相同,尽管它们具有相似的乘法值。因此,我想要一个将这个乘积值与字典项相匹配的函数,以便对其进行评分。这里输出 typeError: 'int' object is not iterable 的代码:

    scoremapDict = {(1, 1): 1, (1, 2): 2, (2, 1): 2, (1, 3): 3, (3, 1): 3, (1, 4): 4, (4, 1): 4,      (2, 2): 4, (1, 5): 5,
  (5, 1): 5, (4, 2): 8, (2, 4): 8, (2, 3): 6, (3, 2):6, (3, 3): 9, (2, 5): 10, (5, 2):10, (3,    4): 12,
  (4, 3):12, (3, 5): 15, (5, 3): 15, (4, 4): 16, (4, 5): 20, (5, 4): 20, (5, 5): 25}


    x = 5
    y = 2
    results = int(x*y)
    for i in results:
     if results == scoremapDict[(x,y)]:
        print("High")
    else:
        print("None")

任何人都可以帮助这个 tyro python-er,在此先感谢:

标签: pythondictionary

解决方案


推荐阅读