带有超时的Python主循环+子循环

时间:2018-11-05 13:19:01

标签: python rfid

我想实现以下目标。

我有正在工作的概念证明。 我有个人“命名RFID”卡,然后有“行动RFID卡”。 所以我可能有这样的卡片:

Names 

John - 12345
Mary - 12346

Actions

Start Work - 111
Finish Work - 222
Lunch - 333

John刷了自己的卡片,然后刷一张动作卡,记录了他的动作。

-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

我正在重用来自的代码:

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

和一个Python游戏示例,该示例一直等待并循环播放游戏

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

我的测试代码如下(我现在不进行卡片或动作查找,期望用户为12345,卡片为54321 :(要求缩进四个空格可能破坏了Python缩进)

#
# 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!")

但是脚本没有退出,而是等待...

感谢任何反馈-我具有基本的python技能,并在可能的情况下尝试重用现有代码(感谢创建者!)。

1 个答案:

答案 0 :(得分:1)

python input()函数将始终等待键盘响应后再返回。看看this answer,了解一种实现所需功能的技术。