Python 属性错误:';功能';对象没有属性';更换';
我正在使用此代码尝试替换字符:Python 属性错误:';功能';对象没有属性';更换';,python,function,object,replace,attributeerror,Python,Function,Object,Replace,Attributeerror,我正在使用此代码尝试替换字符: from another_test import test_once_more test_once_more() question = input("1 letter ") for letter in question: if letter == "a": test_once_more.replace("1","a") print (test_once_more) 这是我正在使用的代码。我所要做的就是替换此代码中的1 de
from another_test import test_once_more
test_once_more()
question = input("1 letter ")
for letter in question:
if letter == "a":
test_once_more.replace("1","a")
print (test_once_more)
这是我正在使用的代码。我所要做的就是替换此代码中的1
def test_once_more():
print ("123456789")
并将其替换为一个
“A”
这里有一个非常简单的错误test\u once\u more
是函数名,而question
是字符串名。尝试回答问题。替换(并修复其他类似错误)
p.S.字符串不能在Python中进行变异,因此需要将调用replace的结果分配给另一个变量,以便查看调用replace()的效果。您不能
该函数正在打印内容并返回None
。事实发生后,这是无法改变的
您应该做的是让函数返回一个值并处理该值:
然后
from another_test import test_once_more
result = test_once_more()
question = input("1 letter ")
for letter in question:
if letter == "a":
result = result.replace("1","a")
print (result)
尽管我对您为什么要使用
for
循环来迭代一个将是单个字符的字符串感到困惑(至少如果您的用户遵循您的请求的话)
from another_test import test_once_more
result = test_once_more()
question = input("1 letter ")
for letter in question:
if letter == "a":
result = result.replace("1","a")
print (result)