int() 参数必须是字符串或实数而不是 X [已修复]

目录

int() argument must be a string or real number not NoneType

  1. int() 参数必须是字符串或实数而不是 NoneType
  2. int() 参数必须是字符串或实数而不是 ‘list’

int() 参数必须是字符串或实数而不是 NoneType

None当我们将值传递给类时,会出现 Python“TypeError: int() argument must be a string, a bytes-like object or a real number, not ‘NoneType’” int()

要解决该错误,请更正分配或提供回退值。

int 参数必须是字符串或实数而不是非类型

下面是错误如何发生的示例。

主程序
example = None # ⛔️ TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType' result = int(example)

None我们将一个值
传递给导致错误的
int()类。

Python 中 None 的常见来源

最常见的None价值来源是:

  1. 有一个不返回任何东西的函数(None隐式返回)。
  2. 将变量显式设置为None.
  3. 将变量分配给调用不返回任何内容的内置函数的结果。
  4. 具有仅在满足特定条件时才返回值的函数。

如果变量存储 None,则提供回退值

解决错误的一种方法是提供回退值,例如,0如果变量存储None.

主程序
example = None result = int(example or 0) print(result) # 👉️ 0

如果变量存储 None,则从0表达式返回。

不返回任何内容的函数返回 None

不显式返回值的函数 return None

主程序
# 👇️ this function returns None def get_str(): print('100') # ⛔️ TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType' result = int(get_str())

您可以使用return 语句从函数返回值。

主程序
def get_str(): return '100' result = int(get_str()) print(result) # 👉️ 100

该函数现在返回一个字符串,我们可以将其转换为整数。

在调用 int() 之前检查变量是否不存储 None


如果在将变量传递给类之前
if需要
检查变量是否未存储 None 值,请使用语句int()

主程序
example = None if example is not None: result = int(example) print(result) else: # 👇️ this runs print('variable stores a None value')

或者,您可以将变量重新分配给后备值。

主程序
example = None if example is None: example = 0 result = int(example) print(result) # 👉️ 0

只有满足条件才返回值的函数

错误的另一个常见原因是具有仅在满足条件时才返回值的函数。

主程序
def get_num(a): if a > 15: return a my_num = get_num(10) print(my_num) # 👉️ None

if函数中的块get_num在传入的数字大于 时运行15

在所有其他情况下,该函数不返回任何内容并最终隐式返回None

要解决错误,您要么必须检查函数是否未返回
None,要么在不满足条件时返回默认值。

主程序
def get_num(a): if a > 15: return a return 0 # 👈️ return fallback if condition not met my_num = get_num(10) print(my_num) # 👉️ 0

现在,无论条件是否满足,函数都保证返回一个值。

int() 参数必须是字符串或实数不是,’list’

当我们将列表传递给类时,会出现 Python“TypeError: int() argument must be a string, a bytes-like object or a real number, not ‘list’” int()

要解决该错误,请访问列表中的特定项目并将该项目传递给类int(),例如int(my_list[0]).

int 参数必须是字符串或实数而不是非类型

下面是错误如何发生的示例。

主程序
my_list = ['3', '5', '8'] # ⛔️ TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list' result = int(my_list)

我们将整个列表传递给int()导致错误的类。

在索引处访问列表

解决错误的一种方法是访问特定索引处的列表并将该项目传递给类int()

主程序
my_list = ['3', '5', '8'] result = int(my_list[0]) print(result) # 👉️ 3
索引是0基于索引的,因此列表中的第一项的索引为0,最后一项的索引为-1

将列表项连接成一个字符串

如果您打算将列表中的所有项目连接成一个字符串并将结果转换为一个整数,请使用该join()方法。

主程序
my_list = ['3', '5', '8'] my_str = ''.join(my_list) print(my_str) # 👉️ '358' my_int = int(my_str) print(my_int) # 👉️ 358

str.join方法将一个可迭代对象作为参数并返回一个字符串,该字符串是可迭代对象中字符串的串联

TypeError请注意,如果可迭代对象中有任何非字符串值,该方法将引发 a 。

如果您的列表包含数字或其他类型,请在调用之前将所有值转换为字符串join()

主程序
my_list = ['3', 5, 8] my_str = ''.join(map(str, my_list)) print(my_str) # 👉️ '358' my_int = int(my_str) print(my_int) # 👉️ 358

map ()函数将一个函数和一个可迭代对象作为参数,并使用可迭代对象的每个项目调用该函数。

将列表中的所有项目转换为整数


如果需要将列表中的所有项目转换为整数,
也可以
使用列表理解。

主程序
my_list = ['3', '5', '8'] new_list = [int(x) for x in my_list] print(new_list) # 👉️ [3, 5, 8]

我们将列表中的每个字符串传递给类int()以将每个项目转换为整数。

额外资源

您可以通过查看以下教程来了解有关相关主题的更多信息: