[英]def in python not working when called
import pyttsx
import random
import os
import serial
import webbrowser
while True:
def portport():
ser = serial.Serial('COM3',9600)
raw_data = ser.read(7)
msg = str(raw_data[3:6])
print msg
ser.close()
engine = pyttsx.init()
portport()
if(msg == 'mof'):
engine.say('Are you sure?')
engine.runAndWait()
portport()
if(msg == 'yes'):
engine.say('Chrome has now closed')
os.system("TASKKILL /F /IM chrome.exe")
engine.runAndWait()
elif(msg == 'noo'):
engine = pyttsx.init()
engine.say('Are you kidding me?')
engine.runAndWait()
if(msg == 'hi_'):
greeting()
if(msg == 'bye'):
leaving()
if(msg == 'who'):
engine = pyttsx.init()
engine.say('Hi there')
engine.runAndWait()
if(msg == 'lik'):
engine = pyttsx.init()
engine.say('Ofcourse I do!')
engine.runAndWait()
我的問題是,如果不portport()
def( portport()
)並在需要時調用它,而是編寫包含在此def中的代碼,則一切正常。 我使用了其他功能(例如,因為不想讓它成為很長的代碼而沒有在這里使用)其他功能(例如greeting()
),並且效果很好。
使用上面的代碼時,Eclipse的控制台“說”:
Traceback (most recent call last):
File "C:\Users\xxx\eclipse\workspace\Ai_Project\Ai\Ai_Brain.py", line 126, in <module>
if(msg == 'mof'):
NameError: name 'msg' is not defined
並且每一個if(msg == 'something'):
變成紅色
我在這里和到處都讀了很多文章,但似乎不是python函數問題
答案:謝謝大家的寶貴回答,也感謝mhawke的回答。 該代碼在必須根據您的建議進行操作之后才起作用。 為了將來使用,更新的代碼是:
.....
def portport():
ser = serial.Serial('COM3',9600)
raw_data = ser.read(7)
msg = str(raw_data[3:6])
print msg
ser.close()
return msg
while True:
engine = pyttsx.init()
msg = portport()
if(msg == 'mof'):
engine.say('Are you sure?')
engine.runAndWait()
msg = portport()
if(msg == 'yes'):
engine.say('Chrome has now closed')
os.system("TASKKILL /F /IM chrome.exe")
engine.runAndWait()
elif(msg == 'noo'):
engine = pyttsx.init()
engine.say('Are you kidding me?')
engine.runAndWait()
......
msg
僅在portport
函數中定義。 如果要在外部使用它,則需要從那里返回它,並將其分配給局部變量。
注意,while循環中不應包含函數定義。
def portport():
ser = serial.Serial('COM3',9600)
raw_data = ser.read(7)
msg = str(raw_data[3:6])
print msg
ser.close()
return msg
while True:
engine = pyttsx.init()
msg = portport()
if msg == 'mof':
...
另請注意,您不需要在Python條件周圍加括號。
msg
是portport()
局部變量,因此無法在外部訪問。
您的msg變量不在正確的范圍內。
import pyttsx
import random
import os
import serial
import webbrowser
while True:
def portport():
ser = serial.Serial('COM3',9600)
raw_data = ser.read(7)
msg = str(raw_data[3:6])
print msg
ser.close()
return msg
engine = pyttsx.init()
msg = portport()
if(msg == 'mof'):
engine.say('Are you sure?')
engine.runAndWait()
portport()
if(msg == 'yes'):
engine.say('Chrome has now closed')
os.system("TASKKILL /F /IM chrome.exe")
engine.runAndWait()
elif(msg == 'noo'):
engine = pyttsx.init()
engine.say('Are you kidding me?')
engine.runAndWait()
if(msg == 'hi_'):
greeting()
if(msg == 'bye'):
leaving()
if(msg == 'who'):
engine = pyttsx.init()
engine.say('Hi there')
engine.runAndWait()
if(msg == 'lik'):
engine = pyttsx.init()
engine.say('Ofcourse I do!')
engine.runAndWait()
問題在於,使用該函數時, msg
是portport()
的局部變量。 該變量在函數外部不可用。 因此,從函數外部對其的引用會導致NameError
異常。
在行中聲明時, msg
的作用域與if
語句中對其的引用相同,因此沒有問題。
使用該函數時糾正此問題的一種方法是讓該函數返回 msg
的值:
def portport():
ser = serial.Serial('COM3',9600)
raw_data = ser.read(7)
msg = str(raw_data[3:6])
print msg
ser.close()
return msg
然后調用portport()
並將返回值分配給變量msg
:
msg = portport()
現在,將在與引用它的if
語句相同的作用域中創建一個新變量msg
。
注意:不要將函數內部的msg
與函數外部的msg
混淆-它們是不同的變量,因為它們存在於不同的作用域中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.