0

I would like to achieve the following.

I have a proof of concept I am working. I have Individual "Named RFID"Cards, then I have "Action RFID Cards". So I might have cards like this:

Names 

John - 12345
Mary - 12346

Actions

Start Work - 111
Finish Work - 222
Lunch - 333

So John Swipes his own card, then swipes an action card, which logs his action.

-Start Script
-Wait for User Card Input
-Once Input Received and Validated
    - Wait for Action Card Input
    - Start Timer
    - Wait until Action Card Input matches a valid Action
    - If a match, exit back to the main loop
    - If no match, wait for one minute, then exit
-Continue Main Loop

I am reusing code from :

How would I stop a while loop after n amount of time?

import time
timeout = time.time() + 60*5   # 5 minutes from now
    while True:
    test = 0
    if test == 5 or time.time() > timeout:
        break
    test = test - 1

and a Python Game example which waits and loops forever playing the game

https://dbader.org/blog/python-intro-reacting-to-user-input

My code for testing is as follows (I am not doing a card or action lookup at this point, expecting the user to be 12345 and card to be 54321: (the requirement for four spaces for indent has possibly broken Python Indent)

#
# Guess My Number
#

import random
import time

# Set our game ending flag to False
game_running = True

while game_running:
    # Greet the user to our game
    print()
    print("I'm thinking of a number between 1 and 10, can you guess it?")

    # Have the program pick a random number between 1 and 10
    #secret_number = random.randint(0, 10)
    secret_number = 12345
    card_number_list = 54321
    # Set the player's guess number to something outside the range
    guess_number = -1

    # Loop until the player guesses our number
    while guess_number != secret_number:

        # Get the player's guess from the player
        print()
        guess = input("Please enter a number: ")

        # Does the user want to quit playing?
        if guess == "quit":
            game_running = False
            break

        # Otherwise, nope, the player wants to keep going
        else:
            # Convert the players guess from a string to an integer
            guess_number = int(guess)


        # Did the player guess the program's number?
        if guess_number == secret_number:
            print()
            print("Hi you have logged on, please swipe Card- if you don't Swipe - will timeout in 1 minute!")

            timeout = time.time() + 60*1   # 1 minutes from now
            while True:
                test = 0
                if test == 1 or time.time() > timeout:
                    card = input("Please enter your card number")
                    card_number = int(card)
                    if card_number == card_number_list:
                        print("Thanks for your card number")
                        test = 1
                break
                test = test - 1
            # Otherwise, whoops, nope, go around again
            else:
                print()
                print("You need to use your wrist band first...")

# Say goodbye to the player
print()
print("Thanks for playing!")

But instead of exiting, the script waits...

Any feedback appreciated - I have basic python skills and am trying to reuse existing code where possible (with thanks to the creators!).

Pang
  • 8,605
  • 144
  • 77
  • 113
Gripsiden
  • 367
  • 1
  • 12

1 Answers1

1

The python input() function will always wait for response from the keyboard before returning. Take a look at this answer for a technique to accomplish what you want.

Jason Jenkins
  • 4,939
  • 3
  • 22
  • 29