简体   繁体   English

当我尝试使用函数参数作为列表索引时,出现错误“列表索引必须是整数或切片,而不是元组”

[英]Error “list indices must be integers or slices, not tuple” occurs when I try to use function parametrs as list indices

I try to use parameters of a function as a list indices, however I get 我尝试将函数的参数用作列表索引,但是我得到了

TypeError: list indices must be integers or slices, not a tuple. TypeError:列表索引必须是整数或切片,而不是元组。

days=['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh']

gifts=['and a patriage in a Pear tree', 'two Turtle Doves', 'three French Hens', 'four Calling Birds', 'five Gold Rings', 'six Geese-a-Laying', 'seven Swans-a-Swimming', 'eigth Maids-a-Milking', 'nine Ladies Dancing', 'ten Lords-a-Leaping', ' eleven Pipers Piping', 'twelve Drummers Drumming']

def recite( start_verse, end_verse):
    return ('On the '+days[start_verse+1] + ' day of Christmas my true love gave to me: '+ gifts[-1, end_verse])

print(recite(2,2))

The problem is here gifts[-1, end_verse] . 问题出在这里gifts[-1, end_verse] You are using a tuple as an index. 您正在使用元组作为索引。

You should use just an integer value instead. 您应该只使用整数值。 Maybe you meant one of these options: 也许您是说以下选项之一:

gifts[-1 + end_verse]
gifts[end_verse - 1]
gifts[-1]
gifts[end_verse]

I think this gifts[-1, end_verse] is the problem. 我认为这个gifts[-1, end_verse]是个问题。 You can't have tuple as a list index. 您不能将元组作为列表索引。 Maybe you wanted gifts[end_verse-1] here? 也许您在这里想要gifts[end_verse-1]

days=['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh']


gifts=['and a patriage in a Pear tree', 'two Turtle Doves', 'three French Hens', 'four Calling Birds', 'five Gold Rings', 'six Geese-a-Laying', 'seven Swans-a-Swimming', 'eigth Maids-a-Milking', 'nine Ladies Dancing', 'ten Lords-a-Leaping', ' eleven Pipers Piping', 'twelve Drummers Drumming']

def recite(start_verse, end_verse):
    return 'On the '+ str(days[start_verse+1]) + ' day of Christmas my true love gave to me: '+ str(gifts[end_verse-1])

print(recite(2,2))

Your return statement is incorrect in the recite function. 您的return语句在recite函数中不正确。 The following code below is probably what you want. 下面的代码可能是您想要的。 This is assuming that recite(1,1) is for the first day, recite(2,2) the second, and so on. 假设第一天是recite(1,1),第二天是recite(2,2),依此类推。

def recite(start_verse, end_verse):
   days=['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh', 'twelfth']
   gifts=['and a patriage in a Pear tree', 'two Turtle Doves', 'three French Hens', 'four Calling Birds', 'five Gold Rings', 'six Geese-a-Laying', 'seven Swans-a-Swimming', 'eigth Maids-a-Milking', 'nine Ladies Dancing', 'ten Lords-a-Leaping', ' eleven Pipers Piping', 'twelve Drummers Drumming']
   return("On the " + days[start_verse-1] + " day of Christmas my true love gave to me: " + gifts[end_verse-1])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试重塑时,它给我一个错误提示“类型错误:列表索引必须是整数或切片,而不是元组” - When i try to reshape, it gives me an error saying "TypeError: list indices must be integers or slices, not tuple" 当我尝试在for循环中调用特定数据时,错误提示:列表索引必须是整数或切片,而不是元组 - When I try to call out specific data in a for loop The error says: list indices must be integers or slices, not tuple “列表索引必须是整数或切片,而不是元组”错误 - "List indices must be integers or slices, not tuple" error 错误:列表索引必须是整数或切片,而不是元组 - Error: list indices must be integers or slices, not tuple 错误是“列表索引必须是整数或切片,而不是元组” - Error is 'list indices must be integers or slices, not tuple' 垂直切片:列表索引必须是整数或切片,而不是元组错误 - Vertical Slices: list indices must be integers or slices, not tuple error 当我尝试运行它时,我收到此错误 TypeError: 列表索引必须是整数或切片,而不是列表 - When I try to run this I get this error TypeError: list indices must be integers or slices, not list 处理列表会出现错误“列表索引必须是整数或切片,而不是元组” - processing a list gives the error "list indices must be integers or slices, not tuple" 列表索引必须是整数或切片而不是元组 - list indices must be integers or slices not tuple TypeError:列表索引必须是整数或切片,而不是元组? - TypeError: list indices must be integers or slices, not tuple?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM