Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中将秒转换为带有大数字的hh:mm:ss时出现问题_Python_Time_Formatting_Numbers_Sys - Fatal编程技术网

在Python中将秒转换为带有大数字的hh:mm:ss时出现问题

在Python中将秒转换为带有大数字的hh:mm:ss时出现问题,python,time,formatting,numbers,sys,Python,Time,Formatting,Numbers,Sys,我让用户输入的输入是秒。大多数时候,我能够将输入转换为hh:mm:ss格式,但当数字变大时,我似乎遇到了一些问题。例如,当我输入900时,得到的结果是01:15:-100。我特别不想要负数,每个数字只想要两个摘要(也不要小数)。我不确定我的数学是否出错了 if sys.argv[1] == "raceTime": n = int(sys.argv[2]) if n < 60: s = format(number, '0>2.0f') print("{}:{}:{}".

我让用户输入的输入是秒。大多数时候,我能够将输入转换为hh:mm:ss格式,但当数字变大时,我似乎遇到了一些问题。例如,当我输入900时,得到的结果是01:15:-100。我特别不想要负数,每个数字只想要两个摘要(也不要小数)。我不确定我的数学是否出错了

if sys.argv[1] == "raceTime":
n = int(sys.argv[2])
if n < 60:
    s = format(number, '0>2.0f')
    print("{}:{}:{}".format(00, 00, s))
else:
    n = int (sys.argv[2])/60
    m =n//1
    h = min//10
    s = (n - h - m)*100
    second = format(s,'0>2.0f')
    minute = format(m, '0>2.0f')
    hour = format(h, '0>2.0f')
    print("{}:{}:{}".format(hour, minute, second))
如果sys.argv[1]=“比赛时间”:
n=int(sys.argv[2])
如果n<60:
s=格式(数字“0>2.0f”)
打印(“{}:{}:{}”。格式(00,00,s))
其他:
n=int(sys.argv[2])/60
m=n//1
h=min//10
s=(n-h-m)*100
秒=格式,'0>2.0f')
分钟=格式(m,'0>2.0f')
小时=格式(h,'0>2.0f')
打印(“{}:{}:{}”。格式(小时、分钟、秒))
正确使用:

或者只是

print("{:02.0f}:{:02.0f}:{:02.0f}".format(hour, min, sec))
# f -> float, 02 -> 2 places before (0-padded), .0 -> 0 places after point

乘以0.25*100得到25。int(1.0)获得1请参见此操作非常有效,谢谢!我唯一的另一个问题是,现在当我输入更大的数字时,比如900,我会得到奇怪的结果。例如:01:15:-100。我该如何解决这个问题?可能重复的
print("{:02.0f}:{:02.0f}:{:02.0f}".format(hour, min, sec))
# f -> float, 02 -> 2 places before (0-padded), .0 -> 0 places after point