诗岑 2019-12-15 16:42 采纳率: 93%
浏览 922
已采纳

NameError: name 'lsx' is not defined

lsx,lsy在函数内定义:

def date():
    lsx=[]

    dates=file.split("\n")
    for date in dates:
        if "-" in date:
            if date.replace("-","").isnumeric()==True:
                p1=date.index('-')#the first -
                p2=date.find('-',p1+1)#the second -
                month=date[p1+1:p2]
                day=date[p2+1:]
                date_on_x=float(month+"."+day)
                lsx.append(date_on_x-50)
def coloring():
    lsy=[]
    lines=file.split("重庆")
    i=0
    for line in lines:

    #index the temprature
        inn=line.index('\n')#The first \n
        inc=line.index("C")#The first C
        if i==0:
            tu=int(line[line.find('\n',inn+1)+1:inc])#The second \n
            if "~" in line:
                tl=int(line[line.index('~')+1:line.rindex('C')])
            else: 
                tl=tu
            i=i+1
        else:
            fn=line.find('\n',inn+1)
            tu=int(line[line.find('\n',fn+1)+1:inc])#The third \n
            if "~" in line:
                tl=int(line[line.index('~')+1:line.rindex('C')])
            else:
                tl=tu
        t=(tl+tu)/2#daily average temprature
        if t<8:
            turtle.color("purple")
        elif 8<=t<12:
            turtle.color("lightblue")
        elif 12<=t<22:
            turtle.color("green")
        elif 22<=t<28:
            turtle.color("yellow")
        elif 28<=t<30:
            turtle.color("orange")
        elif t>=30:
            turtle.color("red")
        lsy.append(t)

在另一个函数那里调用:

def rainbow():

    global lsx,lsy
        #let's draw!
    for i in zip(lsx,lsy):
        turtle.pu()
        turtle.goto(i)
        turtle.pd()
        coloring()
        turtle.circle(10)

报错为:

Traceback (most recent call last):
  File "C:\Users\jyz_1\AppData\Local\Programs\Python\Python37-32\32rx.py", line 105, in <module>
    rainbow()
  File "C:\Users\jyz_1\AppData\Local\Programs\Python\Python37-32\32rx.py", line 93, in rainbow
    for i in zip(lsx,lsy):
NameError: name 'lsx' is not defined

请求解决方案

  • 写回答

1条回答 默认 最新

  • 溪水人家 2019-12-15 20:41
    关注

    如果你想定义全局变量,把lsx lsy定义在函数外部,

    global lsx
    global lsy

    def date()
    处理lsx lxy的代码

    def coloring()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?