1

I am trying to make a timer (Python 3.6.0). I know there are other posts with simpler timers but I want to make a more complex timer. Here is my code:

import time

x = 0

run = input("Start timer? [Y]/[N]")
while run == 'Y':
    time.sleep(1)
    x += 1
    print(x)
else:
    print("end of code")

Is there a way to break the while loop (so that the timer can stop) while the program runs? If I put "in = input()" under the "x += 1" line so that the user can stop the loop, the timer stops counting because the user must provide input every second. If I run the above code without any kind of input I cannot type anything in the Python Shell because the loop keeps going. Is there any way to solve this problem?

9
  • You will need to use threading if you want code to execute while doing an input(). This can get quite complicated quite quickly. Are you sure this is what you need to do? Commented Jan 16, 2017 at 17:37
  • stackoverflow.com/questions/3295938/… Commented Jan 16, 2017 at 17:38
  • See here which in itself is a duplicate, to help get you started. Note: the base duplicate by Alex Martelli seems to be in flux about working across different OS. Commented Jan 16, 2017 at 17:39
  • Another option which is overkill is to use something like pygame, so it'll record your key strokes and just check that during the loop Commented Jan 16, 2017 at 17:40
  • Or you can use select to wait for user input instead of sleeping like in this answer here Commented Jan 16, 2017 at 17:43

1 Answer 1

0

As discussed in comments, something like this might be suitable for you. I'm not quite sure what the timer is supposed to do whilst waiting for input but perhaps something like this helps.

import time

start_timer = input('Do you want to start the timer? Y / N: ')

if start_timer.lower() == 'y':
    print('Type N or n to break the loop')
    start = time.time()
    while start_timer.lower() != 'n':
        start_timer = input()

    end = time.time()
    print('Total loop time: {}'.format(end - start))
else:
    print('Ok, ending')

Edit: The only way to break out of the while loop is to type N or n (the use of .lower() will convert the input to n if it is N). The system time is recorded before entering the loop. The user can then type anything they want for as long as they want, except n. Typing n will then break the loop, the system time is taken again, and the difference between the start and end time is displayed. There is no need for a running timer, just the "before and after" times.

Sign up to request clarification or add additional context in comments.

2 Comments

@BramFran I have changed the code slightly. Now there is only one way to break the loop, allowing the user to continue entering inputs provided that they don't match the input that is set to break the loop (n in this case). I also added an explanation.
This is exactly what I needed! Very clever soloution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.