Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.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 3中将时间转换为整数_Python_Python 3.x_Datetime - Fatal编程技术网

试图在Python 3中将时间转换为整数

试图在Python 3中将时间转换为整数,python,python-3.x,datetime,Python,Python 3.x,Datetime,一般来说,我对Python和编程都是非常陌生的,我已经在这个特定的问题上工作了大约四个小时了。 我试图将时间(例如12:30)转换为“如果”语句中可用的时间。 以下是我迄今为止所尝试的: time = input("Enter the time the call starts in 24-hour notation:\n").split(":") if time >= 8:30 and time <= 18:00: print("YES") time=input(“以24小

一般来说,我对Python和编程都是非常陌生的,我已经在这个特定的问题上工作了大约四个小时了。 我试图将时间(例如12:30)转换为“如果”语句中可用的时间。 以下是我迄今为止所尝试的:

time = input("Enter the time the call starts in 24-hour notation:\n").split(":")
if time >= 8:30 and time <= 18:00:
    print("YES")
time=input(“以24小时表示法输入呼叫开始的时间:\n”).split(:”)

如果时间>=8:30且时间
8:30
不是有效的数据类型。将其转换为整数以使其工作(8:30=8小时,30分钟=8*60+30分钟)

要在几秒钟内获得它,如
12:30:58
,请对最后一行中的
time\u in_sec=time[0]*3600+time[1]*60+time[2]
执行相同的操作

由于模的特性,保证只有一个“实时”时间对应转换为整数的小时。

对于您的问题,请创建一个函数
to_integer(time as_list)
,返回一个int,然后将用户条目与
to_integer('18:00')进行比较。拆分(':')
to_integer('8:30')拆分(':'))
您使用的是冒号,Python需要一个数字或变量名。在这句话中:
如果时间>=8:30,时间=
我对这个问题的看法(没有
datetime
):

answer=input(“以24小时表示法输入通话开始的时间:\n”)
t=元组(int(i)表示答案中的i.split(“:”)

如果(8,30)手动处理时间并不是一件小事。我建议您使用支持时间转换、比较等的
datetime
模块

from datetime import datetime as dt
t = input("...")
t_object = dt.strptime(t, "%H:%M")
if t_object >= dt.strptime("8:30", "%H:%M") and \
   t_object <= dt.strptime("18:00", "%H:%M"):
    do_your_stuff()
从datetime导入datetime作为dt
t=输入(“…”)
t_object=dt.strtime(t,“%H:%M”)
如果t_object>=dt.strtime(“8:30,“%H:%M”),并且\

使用python语法的t_对象,callTime=(int)time应该是callTime=int(time)。但是,不能仅使用int函数将表示时间的字符串转换为int。查看是否将时间解析为整数时间戳。>我不相信我被允许在这个特定问题上使用datetime。您从未说过这是您的硬件分配。politinsa不是OP@killian95没关系。关于
datetime
的备注是在我发布答案后插入的。
answer = input("Enter the time the call starts in 24-hour notation:\n")
t = tuple(int(i) for i in answer.split(':'))

if (8, 30) <= t <= (18, 0):
    print("YES")
from datetime import datetime as dt
t = input("...")
t_object = dt.strptime(t, "%H:%M")
if t_object >= dt.strptime("8:30", "%H:%M") and \
   t_object <= dt.strptime("18:00", "%H:%M"):
    do_your_stuff()