代码不起作用是为什么?
-
老师问一下,我这边代码最后一个else不起作用是为什么?
import sensor, image, time from pyb import UART sensor.reset() sensor.set_pixformat(sensor.RGB565) # grayscale is faster sensor.set_framesize(sensor.QQVGA) sensor.skip_frames(time = 2000) clock = time.clock() uart = UART(4, 115200) while(True): clock.tick() if uart.any(): a = uart.read().decode() b = int(a)-8 img = sensor.snapshot().lens_corr(1.8) for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,r_min = 2, r_max = 100, r_step = 2): img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0)) if c: output_str="[%d]" % (b) uart.write(output_str+'\r\n') else: output_str="[%d]" % (0) uart.write(output_str+'\r\n')
-
代码逻辑有问题。
如果img.find_circles有值,那么c就有值,就会执行if c:里面的程序。
如果img.find_circles是孔的,那么for里面的就不会执行,else就不执行。解决办法:
circles = img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,r_min = 2, r_max = 100, r_step = 2): if circles: for c in circles: img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0)) output_str="[%d]" % (b) uart.write(output_str+'\r\n') else: output_str="[%d]" % (0) uart.write(output_str+'\r\n')
-
此回复已被删除!