【问题标题】:Python- .count not saving variable as int but as 'builtin_function_or_method'Python- .count 不将变量保存为 int 而是保存为 'builtin_function_or_method'
【发布时间】:2020-03-04 03:10:27
【问题描述】:
def reverse(num):
  digits = []
  leng = num.count
  num1 = int(num)
  n = 0
  while(n < leng):
    last = num1 % 10
    num1 = (num1 - last) / 10
    last = str(last)
    digits = digits.append(last)
    n = n + 1

当我运行这个块时,它在 while 循环的行上给出一个错误,说

【问题讨论】:

标签: python int


【解决方案1】:

好吧,根据 shahkalpesh,如果你想找到长度,你可以使用 len(str(num)) 如果您传递的是int

count() 方法适用于字符串string.count(value)。对于字符串,count() 方法返回指定值在字符串中出现的次数,如文档所述:count() for string in python

count() 方法也可用于列表 list.count(value),如文档所述:count() for list in python

所以你有一个错误,因为你试图在一个整数和一个不是整数的对象(或高级数字类)之间使用&lt; 运算符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 2021-03-14
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多