python - 您能检查一下 "unsupported operand type(s) for -: ' str' 和 'int' "

标签 python python-3.x python-2.7

请您检查一下这段代码,为什么 python 中出现“unsupported operand type(s) for -: 'str' and 'int'”这个错误。

代码:

def missing_char(str, n):
  check = len(str -1)
  if check < n or n < 0:
      return False
  else:
      front = str[:n]
      back = str[n+1:]
      return front + back

最佳答案

你把)放在了错误的地方,- 1应该在它的外面:

def missing_char(str, n):
  check = len(str) - 1
  if check < n or n < 0:
      return False
  else:
      front = str[:n]
      back = str[n+1:]
      return front + back

关于python - 您能检查一下 "unsupported operand type(s) for -: ' str' 和 'int' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54274274/

相关文章:

python - 捕获屏幕并找到引用图像

Python:如何将字符串解析为递归字典

python - 静态方法 : get the class name?

python - 将输入附加到现有列表

python - 如何从数据框列中的某些行中删除字符?

python - 如何使用事件触发的 lambda 从 S3 存储桶中获取最新的文件名或文件

Python 3 - 将变量导入字典

python - geopandas overlay() 函数在 QGIS 中不起作用

python - 函数无法识别 args 和 kwargs

python - 在 Pandas DF 中迭代多列并动态切片