鱼C论坛

 找回密码
 立即注册
查看: 1189|回复: 1

[已解决]关于字典报错

[复制链接]
发表于 2021-10-28 21:25:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. from random import randint


  2. def dice():
  3.     a = randint(1, 6)
  4.     b = randint(1, 6)
  5.     c = a + b
  6.     return c


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


  19. if __name__ == '__main__':
  20.     main()
复制代码


在执行这个程序的时候,会报错TypeError: cannot unpack non-iterable int object,不知道是什么原因,请大神指点。
最佳答案
2021-10-28 21:37:17
  1. for num , times in true_frequency :
复制代码

        改为:
  1. for num , times in true_frequency . items() :
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-28 21:37:17 | 显示全部楼层    本楼为最佳答案   
  1. for num , times in true_frequency :
复制代码

        改为:
  1. for num , times in true_frequency . items() :
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 07:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表