我正在 python 上创建一个游戏,它需要歌曲名称才能从中删除一些字符。这是一个例子:abbasong = ('Dancing Queen by ABBA')removed = abbasong.replace("d", "q", "")print removed但是,当我运行程序时,计算机告诉我语法错误,并删除了高亮显示。有谁知道我哪里出错了?
1 回答

BIG阳
TA贡献1859条经验 获得超6个赞
你的abbasong.replace语句有问题。遇到此类问题,最好先咨询口译员的帮助:
replace(self, old, new, count=-1, /)
Return a copy with all occurrences of substring old replaced by new.
count
Maximum number of occurrences to replace.
-1 (the default value) means replace all occurrences.
第三个参数需要是一个整数。它不能是空字符串""。这也是回溯所说的:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer
如果你把一个整数放在那里,它就会像你期望的那样工作。
但如果你面对Syntax Error,它可能在print声明中。Python3 使用,print()而 Python2 使用print. 尝试更改此语句。
但是,大写和小写字符之间也存在差异。“D”与“d”不同,因此removed在您的情况下为空。
添加回答
举报
0/150
提交
取消