Python Forum

Full Version: Perpetual timing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
As another use of a timer, I'm trying to do something like this:
import time

start = time.time()
y = input("What is 4 * 3?")
end = time.time()
while (end-start) < 4:
    if y=="12":
        end = time.time()
        print("it took", (end-start), "seconds")
    else: quit()

start = time.time()
z = input("What is 6 * 6?")
That is, wait until four seconds have elapsed before moving on if not input has been submitted.

Problem is the program stops and waits until input is submitted (when I did enter 12, I saw a running tab of elapsed time until four seconds... pretty cool!).

Is there a way to query continuously whether input has been submitted? If it hasn't then I would have it check time to see if four seconds have passed.
Mark17 Wrote:Is there a way to query continuously whether input has been submitted?
I have a working solution in this post which uses the built-in selectors module.
(Oct-23-2019, 09:52 PM)Gribouillis Wrote: [ -> ]
Mark17 Wrote:Is there a way to query continuously whether input has been submitted?
I have a working solution in this post which uses the built-in selectors module.

Is that OS specific? I run Windows 10 and I got a whole bunch of tracebacks:

C:\Users\drkle\Desktop\Desktop Transfer Material\Miscellaneous\Coursera\Python>timerprogram1.py
Enter passcode: Traceback (most recent call last):
File "C:\Users\drkle\Desktop\Desktop Transfer Material\Miscellaneous\Coursera\Python\timerprogram1.py", line 18, in <module>
main()
File "C:\Users\drkle\Desktop\Desktop Transfer Material\Miscellaneous\Coursera\Python\timerprogram1.py", line 10, in main
pairs = sel.select(timeout=5)
File "C:\Users\drkle\AppData\Local\Programs\Python\Python37-32\lib\selectors.py", line 323, in select
r, w, _ = self._select(self._readers, self._writers, [], timeout)
File "C:\Users\drkle\AppData\Local\Programs\Python\Python37-32\lib\selectors.py", line 314, in _select
r, w, x = select.select(r, w, w, timeout)
OSError: [WinError 10093] Either the application has not called WSAStartup, or WSAStartup failed
Normally, the selectors module has been added to python precisely to write OS independent code. It means that it should work in Windows too. I cannot answer the issue that you're bringing about Windows because I don't know this OS well enough. From my search engine's results, it seems to me that it is not a Python issue. You could perhaps try the fix from this video.