提问人:FireStorm 提问时间:10/13/2022 最后编辑:Ariel GluzmanFireStorm 更新时间:10/14/2022 访问量:778
OverflowError:Python int 太大,无法仅在文件中转换为 C int [重复]
OverflowError: Python int too large to convert to C int in a file only [duplicate]
问:
我的代码:
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
答:
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)
希望我能帮到你。
评论
total_seconds: int | Any = h * 3600 + m * 60 + s
total_seconds
int(total_seconds)
h
m
s