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

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()

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

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题