Python ValueError:要解压缩的值太多(对于具有嵌套变量的循环)

Python ValueError:要解压缩的值太多(对于具有嵌套变量的循环),python,Python,我有以下代码: col_cp1_tariff_time_weekday_1_hour = 0 col_cp1_tariff_time_weekday_2_hour = 7 col_cp1_tariff_time_weekday_3_hour = 9 col_cp1_tariff_time_weekday_4_hour = 19 col_cp1_tariff_time_weekday_5_hour = 21 weekday_cents_questions = [ "What is th

我有以下代码:

col_cp1_tariff_time_weekday_1_hour = 0
col_cp1_tariff_time_weekday_2_hour = 7
col_cp1_tariff_time_weekday_3_hour = 9
col_cp1_tariff_time_weekday_4_hour = 19
col_cp1_tariff_time_weekday_5_hour = 21


weekday_cents_questions = [
    "What is the tariff from the weekday time %d:00? (in cents e.g 23.5)\n" % (col_cp1_tariff_time_weekday_1_hour),
    "What is the tariff from the weekday time %d:00? (in cents e.g 23.5)\n" % (col_cp1_tariff_time_weekday_2_hour),
    "What is the tariff from the weekday time %d:00? (in cents e.g 23.5)\n" % (col_cp1_tariff_time_weekday_3_hour),
    "What is the tariff from the weekday time %d:00? (in cents e.g 23.5)\n" % (col_cp1_tariff_time_weekday_4_hour),
    "What is the tariff from the weekday time %d:00? (in cents e.g 23.5)\n" % (col_cp1_tariff_time_weekday_5_hour)]


print("You will now be asked to enter the cost per kWh for the hourly times in a 24 hour clock.")

variable_bands = [0]

for question in weekday_cents_questions:
    try:
        q = question.format(variable_bands[-1])
        cents = int(input(q))
        variable_bands.append(cents)
    except (SyntaxError, ValueError):
        variable_bands.append(0)

[col_cp1_tariff_time_weekday_1_cents,
 col_cp1_tariff_time_weekday_2_cents,
 col_cp1_tariff_time_weekday_3_cents,
 col_cp1_tariff_time_weekday_4_cents,
 col_cp1_tariff_time_weekday_5_cents] = variable_bands

print(variable_bands)
执行时,我收到错误: ValueError:要解压缩的值太多(预期为5个)


你能告诉我怎么修理吗。我正在尝试将输入的整数分配到变量\u band变量中。

变量\u band=[0]
行中,此列表的大小已经是1。然后,您的代码将另外五个元素附加到
variable\u bands
,导致此列表的大小为6,因此出现
ValueError

相反,将
variable\u bands=[0]
更改为
variable\u bands=[]

但是,在更改之后,您还必须从
for question in weekday\u cents\u questions:
中观察,对于所有的try-except块,它仍然不起作用,因为当列表为空并且您想要获取最后一个列表时,它仍然是一个数组索引超出范围的回溯。相反,您必须删除
question.format()
部分,因为它不是必需的

您的最终代码应该是

variable_bands = []

for question in weekday_cents_questions:
    try:
        cents = int(input(question))
        variable_bands.append(cents)
    except (SyntaxError, ValueError):
        variable_bands.append(0)
这是一个修改程序的运行示例

You will now be asked to enter the cost per kWh for the hourly times in a 24 hour clock.
What is the tariff from the weekday time 0:00? (in cents e.g 23.5)
1
What is the tariff from the weekday time 7:00? (in cents e.g 23.5)
2
What is the tariff from the weekday time 9:00? (in cents e.g 23.5)
3
What is the tariff from the weekday time 19:00? (in cents e.g 23.5)
omg
What is the tariff from the weekday time 21:00? (in cents e.g 23.5)
4
[1, 2, 3, 0, 4]

您应该以空列表的形式启动列表,否则列表中将有6个元素而不是5个元素:

# Start an empty list
variable_bands = []

for question in weekday_cents_questions:
    try:
        # Check if it is empty, otherwise you'll get an IndexError
        if variable_bands == []:
            q = question.format(0)
        else:
            q = question.format(variable_bands[-1])
        cents = int(input(q))
        variable_bands.append(cents)
    except (SyntaxError, ValueError):
        variable_bands.append(0)

variable\u bands
有6个值,您正在尝试将这6个值分配给5个变量
如果variable\u bands=[]
部分甚至不需要。谢谢。是的,我现在看到错误了。谢谢你的帮助。这很有帮助。谢谢