需要一些关于 Python 循环 and/or 重复函数的信息
Need some information with Python loop and/or repeat functions
我是 python 的新手,我已经阅读、搜索、研究和执行过。我在寻找我正在尝试做的事情的答案时遇到问题。这是我所拥有的....
# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.OUT)
def solenoid_on(pin):
GPIO.output(pin, GPIO.HIGH) # Turn solenoid on
def solenoid_off(pin):
GPIO.output(pin, GPIO.LOW) # Turn solenoid off
if __name__ == '__main__':
try:
solenoid_on(channel) # |~ in this section, i would like to repeat X amount
time.sleep(1) # Sets lag time | of times, have a set number of "run time" say...
solenoid_off(channel) # | 150 times..or do i just copy and paste this block
time.sleep(3) # Sets run time | 150 times?
print("DONE")
GPIO.cleanup()
except KeyboardInterrupt:
GPIO.cleanup()
非常感谢您的帮助。
if __name__ == '__main__':
try:
for i in range(150):
solenoid_on(channel) # |~ in this section, i would like to repeat X amount
time.sleep(1) # Sets lag time | of times, have a set number of "run time" say...
solenoid_off(channel) # | 150 times..or do i just copy and paste this block
time.sleep(3) # Sets run time | 150 times?
print("Iteration", i, " is done")
GPIO.cleanup()
您可以试试下面的方法。 For 循环 运行s 通过它 150 次。
如果您特别需要 运行 X 次,请使用 for 循环
如果您需要它在 Y 发生之前一直工作(例如某些响应或时间限制),请使用 while 循环
不确定是否要在 for 循环中使用 try/except。
For循环
if __name__ == '__main__':
try:
for i in range(150):
start = time.time()
print('Turning Solenoid', i, 'On')
solenoid_on(channel)
time.sleep(1)
print('Turning Solenoid', i, 'Off')
solenoid_off(channel)
time.sleep(3)
print('Completed Solenoid', i, 'in', time.time()-start, 'seconds\n\n')
GPIO.cleanup()
except KeyboardInterrupt:
GPIO.cleanup()
While 循环
if __name__ == '__main__':
#minutes elapsed
elapsed = 0
start = time.time()
while elapsed < 10:
try:
print('Turning Solenoid', i, 'On')
solenoid_on(channel)
time.sleep(1)
print('Turning Solenoid', i, 'Off')
solenoid_off(channel)
time.sleep(3)
print('Completed Solenoid', i, 'in', time.time()-start, 'seconds\n\n')
GPIO.cleanup()
except KeyboardInterrupt:
GPIO.cleanup()
# set elapsed minutes
elapsed = (time.time()-start)/60
print('Elapsed time...', elapsed, 'minutes')
我是 python 的新手,我已经阅读、搜索、研究和执行过。我在寻找我正在尝试做的事情的答案时遇到问题。这是我所拥有的....
# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.OUT)
def solenoid_on(pin):
GPIO.output(pin, GPIO.HIGH) # Turn solenoid on
def solenoid_off(pin):
GPIO.output(pin, GPIO.LOW) # Turn solenoid off
if __name__ == '__main__':
try:
solenoid_on(channel) # |~ in this section, i would like to repeat X amount
time.sleep(1) # Sets lag time | of times, have a set number of "run time" say...
solenoid_off(channel) # | 150 times..or do i just copy and paste this block
time.sleep(3) # Sets run time | 150 times?
print("DONE")
GPIO.cleanup()
except KeyboardInterrupt:
GPIO.cleanup()
非常感谢您的帮助。
if __name__ == '__main__':
try:
for i in range(150):
solenoid_on(channel) # |~ in this section, i would like to repeat X amount
time.sleep(1) # Sets lag time | of times, have a set number of "run time" say...
solenoid_off(channel) # | 150 times..or do i just copy and paste this block
time.sleep(3) # Sets run time | 150 times?
print("Iteration", i, " is done")
GPIO.cleanup()
您可以试试下面的方法。 For 循环 运行s 通过它 150 次。
如果您特别需要 运行 X 次,请使用 for 循环
如果您需要它在 Y 发生之前一直工作(例如某些响应或时间限制),请使用 while 循环
不确定是否要在 for 循环中使用 try/except。
For循环
if __name__ == '__main__':
try:
for i in range(150):
start = time.time()
print('Turning Solenoid', i, 'On')
solenoid_on(channel)
time.sleep(1)
print('Turning Solenoid', i, 'Off')
solenoid_off(channel)
time.sleep(3)
print('Completed Solenoid', i, 'in', time.time()-start, 'seconds\n\n')
GPIO.cleanup()
except KeyboardInterrupt:
GPIO.cleanup()
While 循环
if __name__ == '__main__':
#minutes elapsed
elapsed = 0
start = time.time()
while elapsed < 10:
try:
print('Turning Solenoid', i, 'On')
solenoid_on(channel)
time.sleep(1)
print('Turning Solenoid', i, 'Off')
solenoid_off(channel)
time.sleep(3)
print('Completed Solenoid', i, 'in', time.time()-start, 'seconds\n\n')
GPIO.cleanup()
except KeyboardInterrupt:
GPIO.cleanup()
# set elapsed minutes
elapsed = (time.time()-start)/60
print('Elapsed time...', elapsed, 'minutes')