2016-10-12 71 views
0

我爲得到錯誤在我的Python解釋器運行嵌套函數名稱錯誤與嵌套函數

import MySQLdb 
import serial 
import time 
import smtplib 


ser=serial.Serial('/dev/ttyACM1',9600) 
db=MySQLdb.connect("localhost","root","pass","db") 

cursor=db.cursor() 

while 1: 
    print("Waiting ;;...") 
    print("") 
    print("collecting") 
    print("") 

    time.sleep(3) 

    x=ser.readline() 
    time.sleep(3) 
    if x>700: 
    send() 
    print"send mail" 

    print("inserting into Database") 
    sql="INSERT INTO vidit2(temp) VALUES(%s);" %(x) 
    cursor.execute(sql) 
    db.commit() 
    time.sleep(3) 



def send(): 

content="send" 

mail=smtplib.SMTP("smtp.gmail.com",587) 

mail.ehlo() 

mail.starttls() 

mail.login("emailid","pass") 

mail.sendmail("sender","reciever",content) 

mail.close() 

錯誤: 蟒蛇temp.py 等待;; ...

收集

回溯(最近一次通話最後一次): 文件「temp.py」,第24行,在 send() NameError:名稱'send'未定義

請幫忙。 由於提前

回答

1

不同,比如說,JavaScript代碼在編譯時就會"hoist"函數定義,以便他們可以,他們在你的代碼調用之前定義(剛剛得知這個其他的日子,所以原諒我,如果這是一個過於簡單化) ,在Python中,您需要在調用函數之前定義函數(有趣的討論here)。這意味着你需要做的:

def send(): 
... 

前:

send()