不应在for循环中出现值错误?

约翰·萨蒙

这是我的代码:

    while counter <= len(titles):
        currenttime = [perc[counter], fails[counter], titles[counter]]
        print(currenttime)
        for percent, fail, title in currenttime:

当我运行它时,出现一个值错误,显示

ValueError: not enough values to unpack (expected 3, got 2)

但是当我打印当前时间时,我得到

['67', '1', 'subsection']

对我来说,这看起来像3个值,但显然我错了,有人可以启发我吗?我环顾四周,但没有发现良好的答案。任何帮助将不胜感激。谢谢

代码上下文:

      n = 0
    perc = list()
    while n < len(piedata):
        perc.append(piedata[n+2])
        n += 3
    print (perc)

    n = 0
    fails = list()
    while n < len(piedata):
        fails.append(piedata[n+1])
        n += 3
    print(fails)

    n = 0
    titles = list()
    while n < len(piedata):
        titles.append(piedata[n])
        n += 3
    print(titles)

    counter = 0
    while counter <= len(titles):
        currenttime = [perc[counter], fails[counter], titles[counter]]
        print(currenttime)
        for percent, fail, title in currenttime:
            piedata = [percent, (100-percent)]

            fig = matplotlib.figure.Figure(figsize=(5, 5))
            ax = fig.add_subplot(111)
            ax.pie(piedata)  # this is the information that the circle diagram will be made out of
            ax.legend([('amount of attempts:', NOTT), ('amount of fails', fail)])

            circle = matplotlib.patches.Circle((0, 0), 0.7, color='white')
            ax.add_artist(circle)


            # this is the code for actually putting the circle diagram/pie chart on the screen
            canvas = FigureCanvasTkAgg(fig, master=window)
            canvas.get_tk_widget().pack()
            canvas.draw()

            Label(window, text=(title, title), bg='light blue').pack()
            counter += 1

            window.mainloop()
            print(percent)
            print(fail)
吹牛

该声明:

for percent, fail, title in currenttime:

表示要按currenttime顺序解压缩列表中的每个项目,但列表中的每个项目currenttime只是一个字符串,将其解压缩为字符,其中第一项只有两个,导致“没有足够的值要解压缩(预期3 ,得到2)“错误。

为了您的目的,您应该简单地压缩3个列表并遍历zip生成器,而不是while使用带有计数器和内部for循环的循环:

for percent, fail, title in zip(perc, fails, titles):
    piedata = [percent, (100 - percent)]

    fig = matplotlib.figure.Figure(figsize=(5, 5))
    ax = fig.add_subplot(111)
    ax.pie(piedata)  # this is the information that the circle diagram will be made out of
    ax.legend([('amount of attempts:', NOTT), ('amount of fails', fail)])

    circle = matplotlib.patches.Circle((0, 0), 0.7, color='white')
    ax.add_artist(circle)

    # this is the code for actually putting the circle diagram/pie chart on the screen
    canvas = FigureCanvasTkAgg(fig, master=window)
    canvas.get_tk_widget().pack()
    canvas.draw()

    Label(window, text=(title, title), bg='light blue').pack()

    window.mainloop()
    print(percent)
    print(fail)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

set / a在循环中出现错误

在执行循环中出现错误

在执行if循环中的if else条件时,codebuild中出现错误

循环中出现plt.savefig错误

如何避免Postgresql的FOR循环中出现语法错误?

PHP CURL循环中出现400错误

在 for 循环中添加 if 语句会导致“在“}”中出现“意外的 '}””错误

简单的 for 循环中出现意外的令牌错误?

为什么在 SQL Server While 循环中出现错误?

变量中出现值的组

在while循环中出现奇怪的情况

使div在foreach循环中出现的问题

在while循环中编辑int时在C中出现分段错误

在数据帧的简单循环中出现奇怪的不支持操作错误

为什么在低通滤波器循环中出现IndexOutOfBoundsException错误?

在foreach循环中出现“使用未分配的局部变量”错误,并返回收益

在后台将输出重定向到文件时,for循环中出现语法错误

GLSL:由于非常量表达式初始化,For循环中出现错误

如何在“ while”循环中出现错误后返回特定点

查找哈希表的值时,while循环中出现C ++分段错误

在 for 循环中出现 Dict KeyError 但实际上没有错误

使用“长度”在头文件的 For 循环中出现错误 C867

计数继续在列中出现值

提取R中出现值的频率

计算任何列中出现值的行数

在while循环中出现意外的标记“完成”

简单循环中出现意外的列表丢失

如何避免* ngFor循环中出现空格?

JS:我的setTimeout在闭合循环中出现问题