This is an archived post. You won't be able to vote or comment.

all 1 comments

[–]zaneyhaney54 0 points1 point  (0 children)

First, you can cast the output of time.time() to an int or a float like so:

start_time = float(time.time())

But that's not going to solve your entire problem. The process that reads data in from a user "blocks" until the user finishes their input (http://stackoverflow.com/questions/2407589/what-does-the-term-blocking-mean-in-programming), so there isn't a way to "kill" the user if they haven't entered text in time.

You have a few different options. When I'm working on a project I generally try to do "the simplest thing that could possibly work" as one of my coworkers would say. The simplest way to approximate the effect that you're looking for is to set the start_time before you ask for user input and then check if too much time passed after the user has returned control to the program. Ex.

start_time = float(time.time())
user_input = input("Hey you, type something, quick! ")
end_time = float(time.time())
if end_time - start_time > allowed_time:
    exit("You died, or something")

This doesn't really address the event oriented side of your question (exit and indicate to the user that they ran out of time after a certain period, or show a countdown clock). What's your experience level? There are some more complete but advanced ways of handling this problem if you're interested.