Newtopython
诗岑
采纳率78.7%
2019-12-13 15:34

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

  • qq_42034590 吃肉的小馒头 2年前

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

    点赞 评论 复制链接分享