诗岑 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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵