python - 如何使用 LED、按钮命令和时序进行适当的反应时间模拟
问题描述
因此,对于我的反应时间分配,我遇到了多个问题,例如计时器结束时,红灯不会亮起,也不会打印出用户失败的消息,当我在计时器结束后按 Enter 时,我可以仍然按下按钮,它会点亮绿色 LED 并打印出消息。任务的重点是设置 5-10 秒的随机计时器,在黄色 LED 用完之前按下它,并给出用户按下按钮直到它用完的总时间,我想知道如何正确地做到这一点。
import physense_emu
import time
import random
sensor = physense_emu.Sensor()
#Greeting user to press "enter" to start the reaction time simulator
strtTime = "Start"
#Setting up simulator to start when pressing "enter"
butbucket = input("Press Enter")
strtTime = time.time()
randVal = random.randint(5, 11)
sensor.output("yled", "on")
time.sleep(5)
sensor.output("yled", "off")
stpTime = time.time()
btnStp = "Button_3"
#Setting up reaction time system to light up "green" if user is fast enough to push a button, but if not, then it'll light up "red"
while True:
if (sensor.input(btnStp) == "pressed"):
totalTime = stpTime - strtTime
if totalTime < 10:
sensor.output("gled", "on")
time.sleep(3)
sensor.output("gled","off")
print("Congrats, you passed!")
else:
sensor.output("rled", "on")
time.sleep(3)
sensor.output("rled", "off")
print("I'm sorry, but you failed to react quick enough!")
break
解决方案
推荐阅读
- android - Firebase admin SDK Java:未找到所提供电子邮件的用户记录
- javascript - 我在 Angular js 手动引导过程中做错了什么?
- postgresql - PgAdmin 服务器状态空白
- ios - UICollectionViewLayout 将单元格从中心填充到两侧,在一行中
- python - 为什么我的 python 记录器在设置为 DEBUG 时仍会从更高的日志记录级别打印出消息?
- yaml - Swagger 多个示例未显示
- javascript - 纯 JavaScript 捕获链接点击数据
- laravel - 如何在 laravel 的多选复选框列表中显示选中的复选框?
- android - 具有类别、(可选子类别)和子子类别(服务)的数据库
- django - Django REST Framework 序列化程序 PrimaryKeyRelatedField() 不在 GET 响应中添加对象