我遇到了當前Python腳本的問題。 「progress」變量的目的是當它通過一個if循環時獲得一個特定的值。但是,該程序永遠不會超越第一個if語句。它看起來好像每個if語句都有自己的變量叫做'progress'。有人可以幫幫我嗎? 請參閱下面的代碼。在多個if語句中使用相同的Python變量
from bottle import run, route, template, error, static_file
import RPi.GPIO as GPIO
import time
switch1 = 21
switch2 = 20
switch3 = 26
switch4 = 16
switch5 = 19
led1 = 13
led2 = 12
led3 = 6
led4 = 5
led5 = 25
GPIO.setmode(GPIO.BCM)
GPIO.setup(switch1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(switch5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
GPIO.setup(led3, GPIO.OUT)
GPIO.setup(led4, GPIO.OUT)
GPIO.setup(led5, GPIO.OUT)
@route("/")
def hello():
progress = 0
while True:
if progress == 0:
GPIO.output(led1, False)
GPIO.output(led2, False)
GPIO.output(led3, False)
GPIO.output(led4, False)
GPIO.output(led5, False)
progress = 1
return template('index.html')
if (not GPIO.input(switch1)) and progress == 1:
GPIO.output(led1, True)
progress = 2
return template('video1.html')
elif (not GPIO.input(switch2)) and progress == 2:
GPIO.output(led1, False)
GPIO.output(led2, True)
progress = 3
return template('video2.html')
elif (not GPIO.input(switch3)) and progress == 3:
GPIO.output(led2, False)
GPIO.output(led3, True)
progress = 4
return template('video3.html')
elif (not GPIO.input(switch4)) and progress == 4:
GPIO.output(led3, False)
GPIO.output(led4, True)
progress = 5
return template('video4.html')
elif (not GPIO.input(switch5)) and progress == 5:
GPIO.output(led4, False)
GPIO.output(led5, True)
progress = 6
return template('video5.html')
elif progress == 6:
while True:
GPIO.output(led1, True)
GPIO.output(led2, True)
GPIO.output(led3, True)
GPIO.output(led4, True)
GPIO.output(led5, True)
time.sleep(0.5)
GPIO.output(led1, False)
GPIO.output(led2, False)
GPIO.output(led3, False)
GPIO.output(led4, False)
GPIO.output(led5, False)
time.sleep(0.5)
return template('succes.html')
elif GPIO.input(switch1) and GPIO.input(switch2) and GPIO.input(switch3) and GPIO.input(switch4) and GPIO.input(switch5):
time.sleep(0.15)
else:
GPIO.output(led1, False)
GPIO.output(led2, False)
GPIO.output(led3, False)
GPIO.output(led4, False)
GPIO.output(led5, False)
return template('false.html')
time.sleep(0.05)
@route('/<filename>')
def server_static(filename):
return static_file(filename, root='static')
@error(404)
def error404(error):
return("Nothing here, keep searching!")
run(host='0.0.0.0', port=80)
初始分配'進步
0
所以所有的時間== 0'應該是'進度= 0' –那是因爲你有ELIF防止日e代碼進入該塊。將elif更改爲if – karthikr
我看到了,我複製了錯誤的代碼。感謝您的注意! –