诗岑 2019-12-13 15:34 采纳率: 93%
浏览 2586
已采纳

ValueError: too many values to unpack (expected 2)

网上说是元素找不到对应的
代码如下:

import turtle
file=open("C:/Users/jyz_1/Desktop/新建文本文档.txt")
file=file.read()
lines=file.split("重庆")
i=0
lsy=[]
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
    lsy.append(t)
#find the 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=int(month+day)
            lsx.append(date_on_x)

#draw axis
def drawx():
    turtle.pu()
    turtle.goto(-50,-50)
    turtle.pd()
    turtle.fd(240)
def drawy():
    turtle.pu()
    turtle.goto(-50,-50)
    turtle.seth(90)
    turtle.pd()
    turtle.fd(160)
#comment the axis
def comx():
    turtle.pu()
    turtle.goto(-50,-65)
    turtle.seth(0)
    for i in range(1,13):
        turtle.write(i)
        turtle.fd(20)
def comy():
    turtle.pu()
    turtle.goto(-75,-50)
    turtle.seth(90)
    for i in range(-30,51,10):
        turtle.write(float(i))
        turtle.fd(20)
#draw the rainbow
def rainbow():
    #define the color
    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")
    #let's draw!

    for x,t in lsx,lsy:
        turtle.pu()
        turtle.goto(x,t)
        turtle.pd()
        turtle.circle(10)
drawx()
drawy()
comx()
comy()
rainbow()

报错:

Traceback (most recent call last):
  File "C:\Users\jyz_1\AppData\Local\Programs\Python\Python37-32\32rx.py", line 92, in <module>
    rainbow(t)
  File "C:\Users\jyz_1\AppData\Local\Programs\Python\Python37-32\32rx.py", line 83, in rainbow
    for x,t in lsx,lsy:
ValueError: too many values to unpack (expected 2)

但是我用len发现lsx,lsy长度相同
也就是说,lsx,lsy中的元素一一对应
那这个报错是怎么回事?

  • 写回答

1条回答 默认 最新

  • python收藏家 2019-12-17 17:16
    关注

    for x,t in lsx,lsy 这个 应该是要后面加zip
    for x,t in zip(lsx,lsy) 这样就行了

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

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办