[英]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()
My problem is that everything works if instead of creating a def ( portport()
) and call it when I need it, I just write my code which is included in this def. 我的问题是,如果不
portport()
def( portport()
)并在需要时调用它,而是编写包含在此def中的代码,则一切正常。 I used (but didn't put in here because I don't want it to be a very long piece of code) other functions (for example greeting()
) and it worked just fine. 我使用了其他功能(例如,因为不想让它成为很长的代码而没有在这里使用)其他功能(例如
greeting()
),并且效果很好。
Eclipse's console when using the code as above "says": 使用上面的代码时,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
And every single if(msg == 'something'):
turns red 并且每一个
if(msg == 'something'):
变成红色
I have read a lot of posts here and everywhere but can't seem to be a python function problem 我在这里和到处都读了很多文章,但似乎不是python函数问题
THE ANSWER: Thank you all for your valuable answers and a little bit more for mhawke's answer. 答案:谢谢大家的宝贵回答,也感谢mhawke的回答。 The code worked after the chances which had to be made according to your suggestions.
该代码在必须根据您的建议进行操作之后才起作用。 For future use the updated code is:
为了将来使用,更新的代码是:
.....
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
is only defined within the portport
function. msg
仅在portport
函数中定义。 If you want to use it outside, you need to return it from there, and assign it to a local variable. 如果要在外部使用它,则需要从那里返回它,并将其分配给局部变量。
Note, you should not have your function definition within the while loop. 注意,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':
...
Also note, you don't need parentheses around conditions in Python. 另请注意,您不需要在Python条件周围加括号。
msg
是portport()
局部变量,因此无法在外部访问。
Your msg variable is not in the correct scope. 您的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()
The problem is that when using the function, msg
is a local variable of portport()
. 问题在于,使用该函数时,
msg
是portport()
的局部变量。 The variable is not available outside of the function. 该变量在函数外部不可用。 Therefore the references to it from outside the function cause the
NameError
exception. 因此,从函数外部对其的引用会导致
NameError
异常。
When declared in line msg
has the same scope as the references to it in the if
statements, so there is no problem. 在行中声明时,
msg
的作用域与if
语句中对其的引用相同,因此没有问题。
One way to correct this when using the function is to have the function return the value of msg
: 使用该函数时纠正此问题的一种方法是让该函数返回
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
Then call portport()
and assign the return value to the variable msg
: 然后调用
portport()
并将返回值分配给变量msg
:
msg = portport()
Now a new variable msg
is created in the same scope as the if
statements that reference it. 现在,将在与引用它的
if
语句相同的作用域中创建一个新变量msg
。
NB do not confuse the msg
inside the function with that outside the function - they are different variables because they exist in different scopes. 注意:不要将函数内部的
msg
与函数外部的msg
混淆-它们是不同的变量,因为它们存在于不同的作用域中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.