Python主循环+带有timeou的子循环

2024-10-06 14:19:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想实现以下目标。你知道吗

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

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:(indent的四个空格的要求可能破坏了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!")

但是脚本没有退出,而是等待。。。你知道吗

感谢您的反馈-我有基本的python技能,并且正在尽可能重用现有代码(感谢创建者!)。你知道吗


Tags: thetotestgamenumberforiftime