为什么我在Python中使用randint函数会得到这个TypeError?

问题描述 投票:0回答:1

在我从大学获得的 main.py 文件(这是作业的一部分)中,我在尝试运行 main.py 文件时收到此错误消息:

Traceback (most recent call last):
  File "c:\Users\...", line 67, in <module>
    main()
  File "c:\Users\...", line 13, in main
    Ai = [randint(0,1e15) for _ in range(elems)]
          ^^^^^^^^^^^^^^^
  File "C:\Users\...Python\Python312\Lib\random.py", line 336, in randint
    return self.randrange(a, b+1)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\...\Python\Python312\Lib\random.py", line 312, in randrange
    istop = _index(stop)
            ^^^^^^^^^^^^
TypeError: 'float' object cannot be interpreted as an integer

[Done] exited with code=1 in 0.047 seconds

这不应该发生。我确实在应该的地方编写了正确的代码。我不应该在不允许更改的地方出现错误。而且这只是我遇到的问题!我班上的其他人即使使用相同的代码也没有这个问题!

我的Python版本是:Python 3.12.1

我今天确实通过 Windows 安装程序更新了它,然后重新启动了我的电脑。我也删除了早期版本的 Python,但如果出现问题,我不会再次重新启动。

非常感谢任何帮助!

python-3.x windows module floating-point typeerror
1个回答
0
投票

random.randint
应该用整数调用,但 1e15 是浮点数。

尝试一下:

randint(0,10**15)
© www.soinside.com 2019 - 2024. All rights reserved.