timedelta(x,y)在CoCalc.com上返回TypeError,但在其他地方可以工作为什么?

2024-09-29 23:17:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码可以在onlinegdb.com上运行,但不能在CoCalc.com上运行

import datetime
slowduration = datetime.timedelta(0,1)
print(slowduration)

退货

TypeError: unsupported type for timedelta seconds component: sage.rings.integer.Integer

我不清楚这是一个特性还是一个bug


Tags: 代码importcomfordatetimetypetimedeltacomponent
3条回答

雅各布的自我回答是正确的;这里有更多的细节

在SageMath中有一种称为preparser的东西,它解释事物,使整数是数学整数,而不是Python整数。所以for example

sage: preparse('1+1')
'Integer(1)+Integer(1)'

还有更多的事情要做-试试preparse('f(x)=x^2')找些真正的乐趣。但是是的,这是一个特点

不过,要解决Sage内核中的问题,可以执行this

import datetime
slowduration = datetime.timedelta(int(0),int(1))
print(slowduration)

得到0:00:01作为你的答案

为了补充@kcrisman的答案和“int(0), int(1)”技巧

如果想要坚持Sage内核,还有两个选择

  • (1) 用preparser(False)禁用preparser
  • (2) 在整数后面加上r(表示“raw”),例如datetime.timedelta(0r, 1r)

另请参见围绕Sage准备浮点和整数的类似问题和答案:

最后,请注意,可以使用以下任一方法从外部文件将代码加载到Sage中:

  • load('/path/to/filename.py')
  • load('/path/to/filename.sage')

其中.sage文件将获得“Sage preparsed”,而.py文件将不会

这提供了第三个绕过preparser的选项:从.py文件加载代码

如果其他人有这样的问题-结果是我使用的是Sage数学内核,而不是Python数学内核。这个网站提供15种不同的果仁

相关问题 更多 >

    热门问题