2014-07-06 177 views
-1

我正在做一個實習,我需要拿出一個樹莓派,將有一個發聲器添加減1和重置爲0多少天爲公司和該網站事故免費,這裏是我有的代碼,但即時獲得一個無效的語法在While True部分,我沒有編碼經驗,所以任何幫助將非常感謝!python +樹莓pi gpio無效的語法

GPIO.setmode(GPIO.BCM) 
GPIO.Setup (17, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) 
GPIO.Setup (18, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) 
GPIO.Setup (21, GPIO.IN, Pull_up_down = GPIO.PUD_DOWN) 
GPIO.Setup (22, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) 
GPIO.setup (23, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) 
GPIO.Setup (24, GPIO.IN, pull_up_down = GPIO.PUD_DOWN 
    while True: 
     Try: 
     If(GPIO.input(17)== 1): 
      writeTexts(+1,-370,55) 
      writeTexts(today +difference1,-100,-200) 
     If(GPIO.input(18)== 1): 
      WriteTexts(-1,-370,55) 
     If(GPIO.input(21)== 1): 
      writeTexts(0,-370,55) 
     If(GPIO.Input(22)==1): 
      writeTexts(+1,280,55) 
     If(GPIO.input(23)== 1): 
      writeTexts(-1,280,55) 
      writeTexts(today +difference1,550,-200) 
     If(GPIO.input(24)==1): 
      writeTexts(0,280,55) 
      sleep(.01) 
    except KeyboardInterrupt 
     exit() 
GPIO.Cleanup() 

回答

1

請確保您已導入GPIO,time,並在你的代碼頂部的任何模塊:

GPIO.setmode(GPIO.BCM) 
GPIO.Setup (17, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) 
GPIO.Setup (18, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) 
GPIO.Setup (21, GPIO.IN, Pull_up_down = GPIO.PUD_DOWN) 
GPIO.Setup (22, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) 
GPIO.setup (23, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) 
GPIO.Setup (24, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) 

while True: 
    try: 
     if GPIO.input(17) == 1: # all `if` should be lowercase and the `try` 
      writeTexts(+1,-370,55) # don't need `()` in if statements. 
      writeTexts(today + difference1,-100,-200) 
     if GPIO.input(18) == 1: 
      WriteTexts(-1,-370,55) 
     if GPIO.input(21) == 1: 
      writeTexts(0,-370,55) 
     if GPIO.Input(22) == 1: 
      writeTexts(+1,280,55) 
     if GPIO.input(23) == 1: 
      writeTexts(-1,280,55) 
      writeTexts(today + difference1,550,-200) 
     if GPIO.input(24) == 1: 
      writeTexts(0,280,55) 
      sleep(.01) 
    except KeyboardInterrupt: # missing `:` 
     exit() 

GPIO.Cleanup() 
1

你錯過了就行了最終收盤)只是while True前:

GPIO.Setup (24, GPIO.IN, pull_up_down = GPIO.PUD_DOWN 

此外,TryIf應該是小寫。

+1

而'while'和'try'被縮進太多。而且'if's不需要括號。還有其他PEP 8違規行爲。 – icktoofay