Python Forum

Full Version: Have an amount of time to perform and action
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am working on a project that I need to be able to see if you entered the correct passcode in 10 seconds. I have gotten the part to detect if you have entered the correct passcode but can no find out how to make print "you got the code right" if you do enter it in that amount of time and make it print "wrong passcode" if you do not enter any passcode in the 10 seconds or if you do not get the right passcode in that 10 seconds.

Any help would be greatly appreciated, thanks
A simple way to ask for data with a timeout in a console application:
import datetime as dt
import selectors
import sys

def main():
    sel = selectors.DefaultSelector()
    sel.register(sys.stdin, selectors.EVENT_READ)
    print("Enter passcode: ", end='')
    sys.stdout.flush()
    pairs = sel.select(timeout=5)
    if pairs:
        passcode = sys.stdin.readline().strip()
        print('you entered:', passcode)
    else:
        print('\ntimed out')

if __name__ == '__main__':
    main()
A problem with the previous code is that it doesn't hide the passcode. The standard module to hide the password is 'getpass', but it doesn't have a timeout. Now it depends on your framework: which version of python, which OS, are you using a GUI toolkit? GUI toolkits such as tkinter have entry fields that can hide their contents for example and they also have timers. Do you have some code to show?