Newtopython
诗岑
2019-12-15 16:42

NameError: name 'lsx' is not defined

  • python

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条回答

为你推荐