【发布时间】:2018-07-09 20:35:35
【问题描述】:
class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
d=[]
words=s.split()
for i in words:
d.append(i[::-1])
print d
result=" ".join(d)
return result
错误:
UnboundLocalError: local variable 'result' referenced before assignment
编辑:-
result=" ".join(d)
print result
当我在最后两行中使用此代码时,我可以看到我的标准输出但返回的是空值,当我使用这两行代替上面的代码时:
result=" ".join(d)
return result
我只得到该行的第一个单词。
例如。 “我在写代码”
预期输出:-“edoc a gnitirw ma I”
实际输出:“edoc”
【问题讨论】:
-
您的问题到底是什么?你做了什么研究?使用了什么值
s得到错误消息? (特别是,它是空字符串吗?) -
您的代码不会在我的 python 中引发任何异常。