简体   繁体   English

列表索引和无类型错误

[英]List Index & NoneType Error

I have a python list structure which resembles this; 我有一个类似于这个的python列表结构;

seq = ['l', 'm', 'n', 'o', 'p', 'q', 'r', 's']

I increment a pointer so that after certain events in my code I can access the next item of my list (which does not interfere with or mention the list) which is passed into a function to return a and b as below; 我增加了一个指针,以便在代码中发生某些事件后,我可以访问列表的下一项(不干扰或提及列表),该项目传递到函数中以返回a和b,如下所示;

pointer+=1 
a, b= get_instruction(seq[pointer])

I get the error; 我得到了错误;

 TypeError: 'NoneType' object is not iterable

Why is this? 为什么是这样? The variable pointer is set to 0 at the start of the script and it seems to break as soon as I put it to one. 在脚本开始时,变量指针设置为0,并且一旦我将其设置为1,它似乎就会中断。 Pointer increments without an error and can be seen to increment. 指针无错误地递增,并且可以看到递增。 Why will it not simply return the a and b relevant to the item at the pointed to item in the list?? 为什么不简单地返回列表中指向项目的与项目相关的a和b?

I would think this means that get_instructions() is returning nothing. 我认为这意味着get_instructions()不返回任何内容。 It needs to return a proper variable. 它需要返回一个适当的变量。 I can't tell you anything else unless you show us the get_instructions() function. 除非您向我们展示get_instructions()函数,否则我什么都不会告诉您。 What I can tell you is that it is very likely a problem in that function 我可以告诉您的是,该功能很有可能是一个问题

You are trying to unpack a None value into two variables rather than an iterable of length two: 您试图将None值解压缩为两个变量,而不是长度为2的可迭代变量:

a, b = get_instruction(seq[pointer]) # a, b = None

For some reason, your function is evaluating to None. 由于某些原因,您的函数求值为“无”。 You'll have to correct this behavior in the function's contents in order to unpack correctly. 为了正确解压,您必须在函数的内容中更正此行为。 Remember, you'll need an iterable with two items. 请记住,您将需要两个元素进行迭代。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM