python无法识别变量

我正在 python 上创建一个游戏,它需要歌曲名称才能从中删除一些字符。这是一个例子:


abbasong = ('Dancing Queen by ABBA')


removed = abbasong.replace("d", "q", "")


print removed

但是,当我运行程序时,计算机告诉我语法错误,并删除了高亮显示。有谁知道我哪里出错了?

http://img4.mukewang.com/60daec800001661d10250765.jpg

慕虎7371278
浏览 248回答 1
1回答

BIG阳

你的abbasong.replace语句有问题。遇到此类问题,最好先咨询口译员的帮助:replace(self, old, new, count=-1, /)&nbsp; &nbsp; Return a copy with all occurrences of substring old replaced by new.&nbsp; &nbsp; &nbsp; count&nbsp; &nbsp; &nbsp; &nbsp; Maximum number of occurrences to replace.&nbsp; &nbsp; &nbsp; &nbsp; -1 (the default value) means replace all occurrences.第三个参数需要是一个整数。它不能是空字符串""。这也是回溯所说的:Traceback (most recent call last):&nbsp; 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在您的情况下为空。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python