简体   繁体   中英

Attempting to Convert Time Into an Integer in Python 3

I'm extremely new to Python and programming in general, and I've been working at this particular issue for about four hours now. I am trying to convert a time (ex. 12:30) to something usable in an "if" statement. Here's what I've tried so far:

time = input("Enter the time the call starts in 24-hour notation:\n").split(":")
if time >= 8:30 and time <= 18:00:
    print("YES")

When attempting to perform that, I get an invalid syntax error. When I attempt to convert the time to an integer [callTime = int(time)] , I get an error stating that the

int() argument must be a string

This is just a piece of the entire problem I am working on, but I think I can figure out the rest if I can just get a jumping off point with this issue. Although I don't believe I'm allowed to use datetime on this particular problem; anything would help.

EDIT: Corrected int(time)

8:30 is not a valid datatype. Convert it as integer to make it work (8:30 = 8 hours and 30 min = 8*60+30 min)

>>> time = input("Enter the time the call starts in 24-hour notation:\n").split(":")
Enter the time the call starts in 24-hour notation:
12:30
>>> time
['12', '30'] # list of str
>>> time = [int(i) for i in time] # will raise an exception if str cannot be converted to int
>>> time
[12, 30] # list of int
>>> 60*time[0] + time[1] # time in minutes
750
>>> 

To get it in seconds, like 12:30:58 , do the same thing with time_in_sec = time[0] * 3600 + time[1] * 60 + time[2] in the last line.

Due to modulo properties, it's guaranteed that only one "real" time correspond to an hour converted as integer.
For your problem, create a function to_integer(time_as_list) returning an int, and then compare user entry to to_integer('18:00'.split(':')) and to_integer('8:30'.split(':'))

Working with time manually is not trivial. I suggest that you use the datetime module that supports time conversion, comparison, etc.

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()

You are using a colon where Python expects a number or variable name. In this statement: if time >= 8:30 and time <= 18:00: , you need to put the time values in quotes ( "8:30" ) since they are non-numerical. Then, however, you will run into the issue of comparing two non-numerical values with your >= and <= statements. Comparisons only work with actual values, and the colon makes the value into a string instead of an int or float. It would be a better idea to convert the time into an integer by stripping out the colon for comparisons and other manipulation, then you can add the colon back in as necessary.

My take on the problem (without 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM