TypeError:'NoneType' 对象不可迭代 - Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14888736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: 'NoneType' object is not iterable - Python
提问by Nick McArthur
I know this question has been asked before but I can't seem to get mine working. Can someone help me out with this?
我知道以前有人问过这个问题,但我似乎无法让我的工作。有人可以帮我解决这个问题吗?
import sys
class Template:
def processVariable(self, template, data):
first = template.find('{{')
second = template.find('}}')
second = second + 2
out = template[first+second]
assert[out.startswith('{{')]
out = out.strip('{}')
rest = template[second:]
if out in data:
newstring = data[out]
return(rest, newstring)
else:
print "Invalid Data Passed it"
t = Template()
vars = {
'name': 'Jane',
'course': 'CS 1410',
'adjective': 'shimmering'
}
(rest, out) = t.processVariable('{{name}} is in {{course}}', vars)
This is the error I am getting:
这是我得到的错误:
File "template.py", line 28, in <module>
(rest, out) = t.processVariable('{{name}} is in {{course}}', vars)
TypeError: 'NoneType' object is not iterable
I understand the NoneType but is it because of my for loop or did I just miss something simple? Thanks in advance!
我理解 NoneType 但这是因为我的 for 循环还是我只是错过了一些简单的东西?提前致谢!
My code will be passed into a field so that my teacher can run a code against it and it will pass or fail. This is what his code will run:
我的代码将被传递到一个字段中,以便我的老师可以针对它运行代码,它会通过或失败。这是他的代码将运行的内容:
import Candidate
t = Candidate.Template()
vars = {
'name': 'Jane',
'course': 'CS 1410',
'adjective': 'shimmering'
}
(rest, out) = t.processVariable('{{course}} is {{adjective}}', vars)
print 'rest is: [' + rest + ']'
print 'out is : [' + out + ']
what it should return is:
它应该返回的是:
rest is: [ is in {{course}}]
out is : [Jane]
it will return Yes or No if it worked.
如果有效,它将返回 Yes 或 No。
采纳答案by Mark Tolonen
There are errors in your code.
您的代码中有错误。
This:
这个:
out = template[first+second]
assert[out.startswith('{{')]
Should be:
应该:
out = template[first:second]
assert out.startswith('{{')
And:
和:
else:
print "Invalid Data Passed it"
Should probably be:
应该是:
else:
raise ValueError('Invalid data')
Also this functionality is already in Python:
这个功能也已经在 Python 中了:
from string import Template
t = Template('$name is in $course')
vars = {
'name': 'Jane',
'course': 'CS 1410',
'adjective': 'shimmering'
}
print(t.substitute(vars))
Output:
输出:
Jane is in CS 1410
回答by Rohit Jain
You didn't return anything from your elsepart, and hence your function will by default return None.
您没有从您的else部分返回任何内容,因此您的函数默认将 return None。
Ad of course, you cannot unpack a Noneinto two variables. Try returning a tuple from your else part like this:
当然,您不能将 a 解包None为两个变量。尝试从您的 else 部分返回一个元组,如下所示:
if out in data:
newstring = data[out]
return (rest, newstring)
else:
print "Invalid Data Passed it"
return (None, None)

