python error: TypeError: can't multiply sequence by non-int of type 'float'
python error: TypeError: can't multiply sequence by non-int of type 'float'
我的代码有问题。我试图在循环下使用变量 channels_index
一次从列表中获取每个元素而不继续循环,但我遇到了一个错误:TypeError: can't multiply sequence by non-int of type 'float'
当我尝试这个时:
dbconnect = con.cursor()
dbconnect.execute("SELECT COUNT(*) FROM programs")
x = dbconnect.fetchone()[0]
for channels_range, total_channels in enumerate(xrange(1, x + 1, 69), 1):
pass
channels_index = range(0, channels_range) # count how many channels I have got
for ind, row in enumerate(programs):
program = row[1].encode('ascii'), str(row[2]), str(row[3])
title = row[1].encode('ascii')
program_start_date = str(row[2])
program_end_date = str(row[3])
program_height = 33
program_gap = 3
position_top = programs_top + channels_index * (program_height + program_gap + 1.5)
错误在这一行突出显示:
position_top = programs_top + channels_index * (program_height + program_gap + 1.5)
这是一个元素列表:
18:22:32 T:3680 NOTICE: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
我正在连接到数据库以从每 69 行中获取数据以计算我在一个范围内获得了多少行。我不能将 17
的值放在该代码 position_top = programs_top + value
的行中,因为我不知道数据库中有多少 69 行。
你能帮我看看如何在循环中使用变量 channels_index
一次从列表中获取每个元素,而无需继续循环吗?
我不确定我是否完全理解你的要求,但如果你想一次从 channels_index
中获取一个索引,为什么不直接将它添加到你的 for
循环?
# same as above...
for (ind, row), ch_idx in zip(enumerate(programs), channels_index):
# continue on as before
position_top = programs_top + ch_idx * (program_height + program_gap + 1.5)
如果您想 循环 通过值 0 到 channels_range
,然后使用模数:
channels_index = 0
for ...:
channels_index = (channels_index + 1) % channels_range
这会将 channels_index
增加到 1
,然后 2
,一直增加到 channels_range - 1
,然后返回到 0。
此处无需预先生成值列表。
如果你想计算 69 乘以 0,然后 69 乘以 1,以此类推,直到 16,你仍然可以简单地从行索引计算:
for ind, row in enumerate(programs):
channel_index = ind // 69
行索引 0 到 68 除以 69 得到 0
。索引 69 到 137 给出 1
,等等
我的代码有问题。我试图在循环下使用变量 channels_index
一次从列表中获取每个元素而不继续循环,但我遇到了一个错误:TypeError: can't multiply sequence by non-int of type 'float'
当我尝试这个时:
dbconnect = con.cursor()
dbconnect.execute("SELECT COUNT(*) FROM programs")
x = dbconnect.fetchone()[0]
for channels_range, total_channels in enumerate(xrange(1, x + 1, 69), 1):
pass
channels_index = range(0, channels_range) # count how many channels I have got
for ind, row in enumerate(programs):
program = row[1].encode('ascii'), str(row[2]), str(row[3])
title = row[1].encode('ascii')
program_start_date = str(row[2])
program_end_date = str(row[3])
program_height = 33
program_gap = 3
position_top = programs_top + channels_index * (program_height + program_gap + 1.5)
错误在这一行突出显示:
position_top = programs_top + channels_index * (program_height + program_gap + 1.5)
这是一个元素列表:
18:22:32 T:3680 NOTICE: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
我正在连接到数据库以从每 69 行中获取数据以计算我在一个范围内获得了多少行。我不能将 17
的值放在该代码 position_top = programs_top + value
的行中,因为我不知道数据库中有多少 69 行。
你能帮我看看如何在循环中使用变量 channels_index
一次从列表中获取每个元素,而无需继续循环吗?
我不确定我是否完全理解你的要求,但如果你想一次从 channels_index
中获取一个索引,为什么不直接将它添加到你的 for
循环?
# same as above...
for (ind, row), ch_idx in zip(enumerate(programs), channels_index):
# continue on as before
position_top = programs_top + ch_idx * (program_height + program_gap + 1.5)
如果您想 循环 通过值 0 到 channels_range
,然后使用模数:
channels_index = 0
for ...:
channels_index = (channels_index + 1) % channels_range
这会将 channels_index
增加到 1
,然后 2
,一直增加到 channels_range - 1
,然后返回到 0。
此处无需预先生成值列表。
如果你想计算 69 乘以 0,然后 69 乘以 1,以此类推,直到 16,你仍然可以简单地从行索引计算:
for ind, row in enumerate(programs):
channel_index = ind // 69
行索引 0 到 68 除以 69 得到 0
。索引 69 到 137 给出 1
,等等