Python AttributeError: 'NoneType' 对象没有属性 'replace'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24203750/
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
AttributeError: 'NoneType' object has no attribute 'replace'
提问by user3595866
right sorry that I'm not all good at python
but my problem is that i need to replace a character
here is the thing i am trying to change all i need to change is # to an A for all of the lines
很抱歉,我不是都擅长 python,但我的问题是我需要在
这里替换一个字符,这是我想要更改的所有我需要更改的是 # 为所有行的 A
def puzzle():
print ("#+/084&;")
print ("#3*#%#+")
print ("8%203:")
print (",1$&")
print ("!-*%")
print (".#7&33&")
print ("#*#71%")
print ("&-&641'2")
print ("#))85")
print ("9&330*;")
so here is what i attempted to do(it was in another py file)
所以这是我试图做的(它在另一个 py 文件中)
from original_puzzle import puzzle
puzzle()
result = puzzle()
question = input("first letter ")
for letter in question:
if letter == "a":
result = result.replace("#","A")
print (result)
here is what it gives me
这是它给我的
Traceback (most recent call last):
File "N:\AQA 4512;1-practical programming\code\game.py", line 36, in <module>
result = result.replace("#","A")
AttributeError: 'NoneType' object has no attribute 'replace'
it would help if somebody told me a different way around it aswell thanks for the help and sorry again that i'm bad at python
如果有人告诉我一种不同的方法,这会有所帮助,谢谢你的帮助,再次抱歉,我不擅长 python
采纳答案by Mike McKerns
if you don't explicitly return something from a python function, python returns None.
如果您没有从 python 函数中明确返回某些内容,python 将返回 None。
>>> def puzzle():
... print 'hi'
...
>>>
>>> puzzle() is None
hi
True
>>> def puzzle():
... print 'hi'
... return None
...
>>> puzzle() is None
hi
True
>>> def puzzle():
... return 'hi'
...
>>> puzzle()
'hi'
>>> puzzle() is None
False
>>>
回答by Aswin Murugesh
The puzzle()
function does not return anything. That's why you get this error.
该puzzle()
函数不返回任何内容。这就是您收到此错误的原因。