2015-06-09 125 views
0

我遇到了當前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'應該是'進度= 0' –

+0

那是因爲你有ELIF防止日e代碼進入該塊。將elif更改爲if – karthikr

+0

我看到了,我複製了錯誤的代碼。感謝您的注意! –

回答

5

您正在使用if...elif...。 Python將挑選一個匹配測試(第一個ifelif測試來匹配),並且從不運行其他分支。

因此,在其中一個分支中更改progress不會導致其他分支之一被選中。

如果要分開測試,則不應使用elif,而應該爲每個分支使用if

但是,您可以用return語句在每個分支中完全退出視圖。您的功能將不會繼續,循環退出,並且下一個請求將始終再次從頭開始(如果您設置progress = 0。如果progress是爲了在您的服務器中是一個全局狀態,您應該將它設置爲這樣請注意,這不會是線程安全的,如果您使用使用多處理進行擴展的WSGI服務器,該變量也不會跨進程共享。

由於您正在控制一塊硬件,因此使用global可能沒有問題,但是你需要限制你的WSGI服務器只運行一個線程,或者你需要使用鎖定來一次限制這個視圖到一個線程。

爲了使progress全球性的,在你的函數的頂部添加global progress,並把progress = 0功能:

progress = 0 

@route("/") 
def hello(): 
    global progress 
    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') 

注意,while環和sleep()電話都沒有了。您必須在響應中放入Javascript,而不是在超時後重新加載頁面。

+0

我將elif語句更改爲if語句。但是,這並沒有解決問題。它仍然不會通過第一個if語句。 –

+0

@TimonBakker:不,因爲你用'return'語句退出了視圖函數。 –

+0

該網頁設計爲每隔0.2秒重新加載腳本。這種方式仍然是一個有返回聲明的問題嗎? –

0

退出該功能是不是return template('index.html')?由於您始終將progress變量初始化爲第一個,如果始終執行並在結束時退出。

您必須在單獨的函數中對GPIO.output進行初始化,然後才能測試GPIO.input的值。不需要progress變量。

0

由於每瓶文檔return template('')將返回模板,並在停止該函數這個「/」

變量progress是你有沒有不是初始化

分配任何程序內的變量之外的局部變量

已將其分配給第一if語句會被執行

+0

感謝您的回覆!你有一個想法,我可以如何編程文件,使其按照我想要的方式工作? –

+0

現在看到答案@TimonBakker – The6thSense

+0

Thnx爲您的答案。全局變量可能嗎?你能解釋一下如何以正確的方式編程嗎? –