OverflowError:Python int 太大,无法仅在文件中转换为 C int [重复]

OverflowError: Python int too large to convert to C int in a file only [duplicate]

提问人:FireStorm 提问时间:10/13/2022 最后编辑:Ariel GluzmanFireStorm 更新时间:10/14/2022 访问量:778

问:

我的代码:

import time
import datetime
from typing import Any


def countdown(h, m, s):
    total_seconds: int | Any = h * 3600 + m * 60 + s
    minim: int | Any = 0
    while int(total_seconds) > int(minim):
        timer = datetime.timedelta(seconds=int(total_seconds))
        print(timer, end="\r")
        time.sleep(1)
        total_seconds -= 1
    print("Bzzzt! The countdown is at zero seconds!")


h = input("Time in hour :")
m = input("Time in minute :")
s = input("Time in seconds :")
countdown(h, m, s)

当我在窗口终端中执行我的代码时,它工作正常,但是当我把它放在窗口中时,它给了我这个错误:

Traceback (most recent call last):
  File "File", line 1491, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "File", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "File>
    countdown(h, m, s)
  File "File", line 10, in countdown
    timer = datetime.timedelta(seconds=int(total_seconds))
OverflowError: Python int too large to convert to C int
Python 时间 整数

评论

1赞 deceze 10/13/2022
“put it in a window” 是什么意思?
0赞 Karl Knechtel 10/14/2022
提示:代码说的地方,这告诉你关于预期类型的什么?因此,以后是否有必要这样做?但是你这样做了,大概是因为之前有一个不同的错误。那到底是怎么回事呢?下一个提示:应该是整数、和是整数还是字符串?为什么?它们是什么?因此,需要改变什么?total_seconds: int | Any = h * 3600 + m * 60 + stotal_secondsint(total_seconds)hms

答:

2赞 Ariel Gluzman 10/13/2022 #1

你忘了强制转换为 int,之后 ints 和字符串s 混淆了, 改变:

total_seconds: int | Any = h * 3600 + m * 60 + s

自:

total_seconds: int | Any = int(h) * 3600 + int(m) * 60 + int(s)

希望我能帮到你。