2017-05-24 17 views
-2

私はPythonで、次のような問題点がありますが、私は考えている理由::Pythonのさえずり "NameErrorグローバル名 'SERは' が定義されていない"

"Traceback (most recent call last): File 
C:/Users/W7-32/Downloads/TW31.py", line 82, in <module> 
    driptwit() ## Llamada a la funcion driptwit File "C:/Users/W7-32/Downloads/TW31.py", line 71, in driptwit 
    ser.write("1") NameError: global name 'ser' is not defined 

ソースコード:

# lIBRERIAS 

import twitter 
import tweepy 
import serial 
import time 
import threading 
import os 
import datetime 

##Consumer y Access de Twitter Developers from tweepy import API 
consumer_key = 'QpaLpmQOS0DMTt2hFxy5WN40i' 
consumer_secret = 'WoUssPI9gIdiEIu0LSWG2bvGuAlvF8WTDStdFTkj5OIPwbeLth' 
access_token_key = '935906436-aMBTKoPOZshZhCRRil1vNCTUKPxQBSiuXe181hLp' 
access_token_secret = 'pxYs26cMqxQbhxQtgfIGnUjWGP1eLlK9mVxdaLeorPfZ4' 

#Autenticacion y conexion con Twitter 
#auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
##auth.set_access_token(access_token_key, access_token_secret) 

#api = tweepy.api(consumer_key, consumer_secret, access_token_key, >  access_token_secret) 
#api=tweepy.API(auth) 
##Declaracion del puerto serial 

auth=tweepy.OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token_key, access_token_secret) 

api=tweepy.API(auth) 


locations = ['COM3','COM4','COM5','COM6'] 

for device in locations: 

    try: 

     ser = serial.Serial(device, baudrate=9600, bytesize=8, 

     parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE) 

     print ("\nArduino found on: " + ser.portstr + " please >  wait...\n") 
     time.sleep(2) 
     break 
    except: 
       print "Failed to connect on", device 

## check serial port 
def checkokay(): 
    ser.flushInput() 
    time.sleep(3) 
    line = ser.readline() 
    time.sleep(3) 
    if line == ' ': 
     line = ser.readline() 
    print("here") 


print('Bienvenido a Print_Twitter') 


def driptwit(): 
    status = [] 
    x = 0 
    status = api.user_timeline('FerCMejia') ##grab latest statuses 
    checkit = [s.text for s in status] ##put status in an array 
    drip = checkit[0].split() ##split first tweet into words 

    ## check for match and write to serial if match 
    if (drip[0] == '#dripPrint'): 
     print("Recibiendo Twitter, Imprimiendo") 
     ser.write("1") 
    else: 
     if drip[0] == '#dripPrintStop': ##break if done 
      ser.write("0") 
      print("Impresion detenida, esperando instruccion") 
     else: 
       print("Esperando Tweet") 
       ser.write("0") 


while 1: 
     driptwit() ## Llamada a la funcion driptwit 
     time.sleep(20) ## Espera de 20 seg para volver a leer el status 

ポートにはArduinoが接続されていますが、それは私には定義されていないというエラーを与えます。

Archive Python

私はPycharmとPython2.7インタプリタ、のWindows7 32ビットを使用します。

は、ここでの.pyアーカイブです。

+4

あなたのコードをあなたの質問に示してください。ほとんどの場合、スペルが間違っているか範囲外の名前を使用しようとしている可能性があります。 – cdarke

+0

Seconded。メガのようなリンクが不審であるという事実に加えて、あなたは社会が自由に助けるためにその時間を与えることを考えると、むしろ同じことをやって私たちの時間を過ごすに私たちを尋ねるよりも、問題を特定するために[MCVE]にコードを減らす必要があります。 –

+0

準備完了、それが正しい?ありがとう!! –

答えて

0

serはグローバル変数ではありません。 forループの中で定義すると、その範囲はそのループに限定されます。 forループの上でこれを行う:

ser = None 
for device in locations: 

これは、その範囲をforループに限定するだけではありません。 forループは、ループで変数を呼び出すときに定義された変数を使用するようになりました。 はその後、機能の本体に、あなたが呼び出すときserそれは新しいものを作成しようとしているグローバル変数ではなくなってきたことを確認するために先頭にglobalキーワードを追加したいと思うでしょう。その他の機能は同じ

def checkokay(): 
    global ser 
    ser.flushInput() 

:このよう

def driptwit(): 
    global ser 
    status = [] 

今、あなたの変数はグローバルであり、あなたの関数は、変数のグローバルバージョンを使用しています。

関連する問題