2

I need a loop for a game that will run until one minute is up. I have already searched and I found this:

t_end = time.time() + 60
while time.time() < t_end:
    #blah blah blah

But this loop will run for one minute and then finish the loop it's on. I need a loop for my game that will stop as soon as the time runs out so the person doesn't get to guess again before the loop is over.

klaytonme
  • 83
  • 8
  • 1
    do you have something inside your loop that's waiting for user input? If so, please include that in your snippet as it's very relevant. – Hamms Apr 25 '16 at 23:58
  • 1
    The only way to break a task that is currently executing, without waiting for the loop to finish, is to use a thread or multiprocessing. Please be warned, multiprocessing can be complex for beginners, and considerations should be taking when using threading. But it's the only way to ensure that the program terminates after exactly 1 minute. See the docs here: https://docs.python.org/2/library/threading.html https://docs.python.org/2/library/multiprocessing.html – Alex Huszagh Apr 26 '16 at 00:02
  • You give a code snippet for a loop that stops as soon as the time runs out, and then say that you "need a loop for my game that will stop as soon as the time runs out." What are you trying to do, really? – Paul Cornelius Apr 26 '16 at 00:11
  • 1
    @PaulCornelius Not really, he said that this loop will run for a minute and then finish the current loop: if the minute ends during the loop, then you'll have to wait for the next one to really terminate the program. But he want something that stops really after a minute. The thing is, that the `blah blah blah` in the code can be very long to execute. – nasso Apr 26 '16 at 00:17
  • @Nasso I think you're right. That would make sense and it's a real problem. A nice job of reading between the lines. – Paul Cornelius Apr 26 '16 at 02:38
  • 1
    I'm really sorry, I forgot to mention something very very important, there is an input statement inside the loop that waits for a user input, so waiting for the end of the loop to terminate the program is not an option. The only way I could think of was multiprocessing, but I wanted to see if you guys knew another way. I'm sorry I forgot to mention that. – klaytonme Apr 26 '16 at 14:37
  • 1
    @klayton If you're the person downvoting all these answers, and if the reason for the downvotes is because the answers didn't solve the problem that you failed to describe correctly, you ought to fix that. – Paul Cornelius Apr 27 '16 at 21:54
  • @klayton If you call `input` from the main thread - and I'm not sure if you can get away with calling it from another thread - then you will have to deal with the issue of forcing a program exit from a secondary thread. See this post: http://stackoverflow.com/questions/73663/terminating-a-python-script. Use of the built-in `input` function is often problematic. – Paul Cornelius Apr 27 '16 at 21:57
  • I agree with what @PaulCornelius is saying regarding the downvotes. There's no point in downvoting everyone. It won't mean that you'll get an answer that answers your question. Quit with the downvotes. – gotnull Apr 29 '16 at 01:21
  • For obvious reasons I just downvoted the question. – Paul Cornelius Apr 29 '16 at 20:46
  • I am not down voting anything. I have already acknowledged that it is my fault for asking the question wrong. – klaytonme Apr 29 '16 at 23:54

2 Answers2

-1

Perhaps what you want is to simply check if the user has been idle for one minute. You can't do that with a stand-alone timing loop because the rest of your program has no way of responding to the timeout, unless you use multiple threads (which you probably don't want to do). In a single-threaded program you have to mix the time-checking in with the rest of your logic, something like (pseudocode):

last_user_action = time.time()
while True:
    if user_did_something():
        last_user_action = time.time()
    else:
        if time.time() - 60.0 > last_user_action:
            break
    blah_blah_blah()  # You must keep this fairly short
sys.exit(1)

If your function blah_blah_blah is short enough this will work OK. If it isn't you have to figure out how to write a multithreaded or event-driven application. Tkinter or any GUI environment has the necessary functions.

Paul Cornelius
  • 9,245
  • 1
  • 15
  • 24
-3

You will want to have a check inside your for loop to break it.

import time
t_end = time.time() + 60
while time.time() < t_end:
    if time.time() >= t_end:
        break
    else:
        #blah blah blah

The greater than condition will ensure that we still break the loop under the condition that the clock ticks were enough to make time.time() greater than t_end between the time the loop started and the time it checked the if statement.