python - 新手程序员寻求指导

标签 python python-3.x debugging error-handling

我需要此程序的帮助,以解决我似乎无法解决的问题

numblist = []

def two23(numblist):

    **if two23([0][1]) == 2:**
        return 'True'

    elif two23([0][1]) == 3:
        return 'True'

    else:
        return 'False'

print(two23([2, 3]))

如果列表的内容是2和2或3和3,则该程序应该输出true。如果列表是2和3,则程序将输出false。
每当我尝试运行此代码时,都会收到一条错误消息,指出加注星标的文本是“超出范围”。请帮助并提前谢谢

最佳答案

[0][1]引用列表[0]的索引1处的元素,但是此列表在索引0处仅包含一个元素,因此您将收到错误消息“IndexError:列表索引超出范围”。你一定不是那个意思

two23([0][1])

甚至是two23([0]),因为这将是一个函数调用。您只需
def two23(numblist):
   return numblist == [2,2] or numblist == [3,3]

无需if语句。您甚至可以使其更简单:
def two23(numblist):
   return numblist in ([2,2],[3,3])

关于python - 新手程序员寻求指导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43648993/

相关文章:

c++ - 使用 "maximize speed"但不是 "minimize size"的堆问题

python - 属性错误 : module 'keras.utils.generic_utils' has no attribute 'populate_dict_with_module_objects'

python - Unicode编码错误: 'charmap' codec can't encode character '\U0001f937' in position 0: character maps to <undefined>

python - 将字符串列表写入txt文件,但文件为空,为什么?

Python:仅使用形状文件的一部分

laravel - 如何查看 Laravel 中的内存使用情况?

.net - 在调试器中格式化第 3 方 .NET 对象

python - if-not 单行以避免重复 block ,这是 python 风格的问题

python - 如何将按钮小部件放在标签小部件上并确定其在 Tkinter 中的位置

python - 如何在Python中绘制晶须图(删除盒须图中的盒子部分)