1

Is there a way to set a 30 second timer to verify a username for a program before it returns back to the start line, and requests for the user to input the name again? This is what I have so far:

print("")
verifyp1loop = True
while verifyp1loop==True:
    verifyp1 = input("Please input Player 1's username. ")
    verifyp1confirm = input("Are you sure you want this to be your username? y/n ")
    if verifyp1confirm == "y":
        print("Username confirmed.")
        verifyp1loop=False
    else:
        print("Username denied.")

verifyp2loop = True
while verifyp2loop==True:
    verifyp2=input("Please input Player 2's username. ")
    verifyp2confirm=input("Are you sure you want this to be your username? y/n ")
    if verifyp2confirm == "y":
        print("Username confirmed.")
        verifyp2loop=False
    else:
        print("Username denied.")

I'm very new to this and any help would be appreciated :)

Orange Man
  • 11
  • 4
  • 3
    Possible duplicate of [Keyboard input with timeout in Python](https://stackoverflow.com/questions/1335507/keyboard-input-with-timeout-in-python) – Andrew F Jan 14 '19 at 12:28
  • You will need some kind of framework for asynchronous code for that, e.g. `asyncio` or `trio`. – L3viathan Jan 14 '19 at 12:31
  • Not duplicate, that keyboard solution works only in Linux – Martin Jan 14 '19 at 17:01

1 Answers1

0

Lightweight solution:

no loops

no threads

just example how to implement timeout

import time
class Verify(object):
    def __init__(self,timeout):
        self.timeout = timeout
        self.verification = None
    def verify(self):
        self.verification = None
        start_verification = time.time()
        verifyp1confirm = input("Are you sure you want this to be your username? y/n ")
        end_verification = time.time()
        if (end_verification-start_verification)>self.timeout:
            print('Denied')
            self.verification = 0
        else:
            print('OK')
            self.verification = 1

>>> ver=Verify(3)
>>> ver.verify()
Are you sure you want this to be your username? y/n y
OK
>>> print(ver.verification)
1
>>> ver.verify()
Are you sure you want this to be your username? y/n y
Denied
>>> print(ver.verification)
0

Note same answer, different output

Implementation:

ver=Verify(3)
while ver.verification == None or ver.verification ==0:
    ver.verify()
Martin
  • 3,333
  • 2
  • 18
  • 39