"NameError: global name 'ser' is not defined" python twitter
"NameError: global name 'ser' is not defined" python twitter
我在 Python 中遇到以下问题,但我不知道为什么:
"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,但它给我一个未定义的错误。
这是 .py 存档:
我使用 Pycharm 和 Python2.7 解释器,Windows7 32 位。
ser
不是全局变量。您在 for 循环内定义它,因此它的范围仅限于该循环。在 for 循环上方执行此操作:
ser = None
for device in locations:
这使得它的范围不仅仅局限于for循环。 for 循环现在将使用在循环中调用它时刚刚定义的那个变量。
然后,在函数体中,您需要在顶部添加 global
关键字,以确保当您调用 ser
时它获取全局变量而不是尝试创建新变量。像这样:
def checkokay():
global ser
ser.flushInput()
其他功能相同:
def driptwit():
global ser
status = []
现在你的变量是全局变量,你的函数正在使用变量的全局版本。
我在 Python 中遇到以下问题,但我不知道为什么:
"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,但它给我一个未定义的错误。
这是 .py 存档:
我使用 Pycharm 和 Python2.7 解释器,Windows7 32 位。
ser
不是全局变量。您在 for 循环内定义它,因此它的范围仅限于该循环。在 for 循环上方执行此操作:
ser = None
for device in locations:
这使得它的范围不仅仅局限于for循环。 for 循环现在将使用在循环中调用它时刚刚定义的那个变量。
然后,在函数体中,您需要在顶部添加 global
关键字,以确保当您调用 ser
时它获取全局变量而不是尝试创建新变量。像这样:
def checkokay():
global ser
ser.flushInput()
其他功能相同:
def driptwit():
global ser
status = []
现在你的变量是全局变量,你的函数正在使用变量的全局版本。