【发布时间】:2019-08-01 14:30:05
【问题描述】:
我在第 20 行 (pulse_duration = end_time - start_time) 收到错误消息:
NameError: name 'start_time' is not defined
import gpiozero
import time
TRIG = 23
ECHO = 24
trigger = gpiozero.OutputDevice(TRIG)
echo = gpiozero.DigitalInputDevice(ECHO)
trigger.on()
time.sleep(0.00001)
trigger.off()
while echo.is_active == False:
start = time.time()
while echo.is_active == True:
end = time.time()
pulse_duration = end_time - start_time
distance = 34300 * (pulse_duration/2)
round_distance = round(distance, 1)
print("Distance: ", round_distance)
【问题讨论】:
-
因为您似乎还没有定义变量
start_time...您确实定义了一个变量start...也许您的意思是? -
您也没有定义
end_time,但您定义了end。
标签: python python-3.x nameerror