【问题标题】:Python Main Loop + Sub Loop with timeoutPython主循环+子循环超时
【发布时间】:2018-11-05 13:19:01
【问题描述】:

我想实现以下目标。

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

Names 

John - 12345
Mary - 12346

Actions

Start Work - 111
Finish Work - 222
Lunch - 333

所以约翰刷了他自己的卡片,然后刷了一张动作卡片,记录了他的动作。

-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 技能,并且正在尝试尽可能重用现有代码(感谢创建者!)。

【问题讨论】:

  • 你真的应该先重新格式化你的代码,这样我们才能阅读它
  • 缩进需要纠正,否则几乎不可能遵循逻辑

标签: python rfid


【解决方案1】:

pythoninput() 函数在返回之前总是会等待键盘的响应。看看this answer 的技术来完成你想要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 2021-10-26
    • 2022-01-06
    • 2020-01-03
    • 2017-11-08
    • 1970-01-01
    相关资源
    最近更新 更多