【发布时间】:2018-08-15 02:13:15
【问题描述】:
def print_guessed(secret_word):
new_word = secret_word
for i in new_word:
i = "-"
return new_word
print print_guessed("claptrap")
这只是打印 claptrap 而不是 -------- 如何解决?
【问题讨论】:
-
return re.sub("[a-zA-Z]","-",secret_word) -
首先,字符串是不可变的,所以
I='-'不会改变new_word中I的值。所以你最终只是将-分配给变量I而什么都不做。你可以做的是new_word = list(secret_word)并遍历位置...for I in range(len(new_word)): new_word[I] = '-'.. 然后重新调整连接的字符串''.join(new_word) -
字符串是不可变的,循环变量根本不是这样工作的,你真的需要一个基础教程。
-
这个
python-requests怎么样?我删除了那个标签。
标签: python python-3.x python-2.7