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

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

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

报告相同问题?

悬赏问题

  • ¥15 求解答:pytorch跑yolov8神经网络受挫
  • ¥20 Js代码报错问题不知道怎么解决
  • ¥15 gojs 点击按钮node的position位置进行改变,再次点击回到原来的位置
  • ¥15 计算决策面并仿真附上结果
  • ¥20 halcon 图像拼接
  • ¥15 webstorm上开发的vue3+vite5+typeScript打包时报错
  • ¥15 vue使用gojs,需求在link中的虚线上添加方向箭头
  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥100 求ASMedia ASM1184e & ASM1187e 芯片datasheet/规格书