【发布时间】:2013-03-14 03:42:38
【问题描述】:
我正在尝试用单词[NOUN] 替换字符串。我一无所知!
下面是我的代码 - 它返回很多错误 - 变量 story 是一个字符串,而 listOfNouns 是一个列表 - 所以我尝试通过拆分将字符串转换为列表。:
def replacement(story, listOfNouns):
length = len(story1)
story1 = story.split()
for c in range(0,len(story1)):
if c in listOfNouns:
story1[c]= 'NOUN'
story = ''.join(story)
return story
以下是我使用 replacement("Let's play marbles", ['marbles']) 调用上述函数时收到的错误消息:
Traceback (most recent call last):
File "<pyshell#189>", line 1, in <module>
replacement("Let's play marbels", ['marbels'])
File "C:/ProblemSet4/exam.py", line 3, in replacement
length = len(story1)
UnboundLocalError: local variable 'story1' referenced before assignment
如何用另一个列表中的另一个元素替换新的 story1 列表?
如何修改元组并返回新字符串 - 应该是:Let's play [NOUN]???
有人可以帮忙吗?我迷路了,我已经尝试了几个小时,使用我在 Python/Java 中的所有知识来解决这个问题!
【问题讨论】:
标签: python string list split replace