【问题标题】:cannot unpack non-iterable float object无法解压不可迭代的浮动对象
【发布时间】:2021-07-10 23:22:54
【问题描述】:

每当我运行我的代码时,我总是收到“无法解压缩不可迭代的浮点对象”的错误,我对错误的来源感到困惑,我是否必须以某种方式使用迭代变量?

def DEADBEEF(n):
    count = 0 
    for i in range(n):
        x ,y = np.random.uniform(0,1)
        if (np.sqrt(x**2 + y**2)<=1):
            count = count + 1
     answer = count/100
     return answer

holder = DEADBEEF(100)

【问题讨论】:

  • 你期望np.random.uniform(0, 1)的返回值是多少?

标签: python python-3.7


【解决方案1】:

np.random.uniform 只要不传递size 参数,就会返回一个浮点数。

如果你想使用x, y = ...,你必须在赋值的右边至少提供两个值。

如果您想使用np.random.uniformxy 分配一个浮点数,请尝试使用size 参数:

x, y = np.random.uniform(0, 1, size=2)

【讨论】:

  • 哇,这么简单的事情,让我卡了一个小时。谢谢!
猜你喜欢
  • 2020-09-10
  • 2020-01-28
  • 2019-04-22
  • 1970-01-01
  • 2020-07-21
  • 2020-04-08
  • 2021-05-25
  • 1970-01-01
相关资源
最近更新 更多