“你能编写程序,测试你的反应时间吗?让程序块的转态指示灯随机变绿一段时间,然后再变红。一看到灯变红,你就要按下触动传感器。然后程序显示从灯变红到按下触动传感器的时间。将整个程序放到一个循环中,看看你的反应时间是否缩短!程序变化后,再扩展它,防止人们作弊,提前按下触动传感器。”

#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import (Port, Stop, Direction, Button, Color,
SoundFile, ImageFile, Align)
from pybricks.tools import print, wait, StopWatch
from pybricks.robotics import DriveBase
# Write your program here
brick.sound.beep()
watch = StopWatch()
tou_sensor = TouchSensor(Port.S1)
import random
while True:
times = random.randint(1000, 5000)
brick.light(Color.GREEN)
wait(times)
brick.light(Color.RED)
watch.reset()
watch.resume()
while tou_sensor.pressed():
wait(1)
while True:
if tou_sensor.pressed():
time = watch.time()
print(time)
break