1005663861 发表于 2021-10-28 21:25:51

关于字典报错

from random import randint


def dice():
    a = randint(1, 6)
    b = randint(1, 6)
    c = a + b
    return c


def main():
    frequency = {2: 2.78, 3: 5.56, 4: 8.33, 5: 11.11, 6: 13.89, 7: 16.67,
               8: 13.89, 9: 11.11, 10: 8.33, 11: 5.56, 12: 2.78}
    true_frequency = {2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0,
               8: 0, 9: 0, 10: 0, 11: 0, 12: 0}
    time = int(input('please enter the times:'))
    for i in range(0, time):
      if frequency.keys() == dice():
            true_frequency += 1
    time = float(time)
    for num, times in true_frequency:
      true_frequency = times / time


if __name__ == '__main__':
    main()


在执行这个程序的时候,会报错TypeError: cannot unpack non-iterable int object,不知道是什么原因,请大神指点。

jackz007 发表于 2021-10-28 21:37:17

for num , times in true_frequency :
      改为:
for num , times in true_frequency . items() :
页: [1]
查看完整版本: 关于字典报错