如何修复TypeError:python中“str”和“str”的操作数类型不受支持

2024-06-26 18:01:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我想减少字符串变量lent的长度。我得到这个错误:

15
Traceback (most recent call last):
  File "script.py", line 25, in <module>
    script()
  File "script.py", line 11, in script
    calc = print(str(lent) - str(1))
TypeError: unsupported operand type(s) for -: 'str' and 'str'

这是我的密码:

for country in countrys:
    key = pg.typewrite('in ' + str(country))
    lent = print(len(str(key) + str(country)))
    calc = print(str(lent) - str(1))

Tags: key字符串inpyfor错误linescript
3条回答
calc = print(len(str(lent)) - 1)

变量lent是一个integer。你只需要减少1

calc = print(lent - 1)

由于您的代码已完全损坏(您通常不为任何内容分配打印返回值),并且您尝试添加/删除stings和ints,目前还不清楚打字机的功能以及您在country和lent中期望的数据类型,我只能假设您想要:

for country in countrys:
    key = 'in ' + country
    pg.typewrite(key)
    calc = len(key) - 1

相关问题 更多 >