为了账号安全,请及时绑定邮箱和手机立即绑定

python无法识别变量

python无法识别变量

慕虎7371278 2021-06-18 18:19:23
我正在 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在您的情况下为空。


查看完整回答
反对 回复 2021-06-29
  • 1 回答
  • 0 关注
  • 247 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号