在 python 中从 float 转换为 int 的值错误(非常奇怪的问题)
Value error converting from float to int in python (very weird problem)
我是 python 的新手。我英语不好。
我enter image description here 得到一个错误“以 10 为底的 int() 的无效文字”
我将一个浮点数从 float 转换为 int 并得到了那个错误。
我不知道该怎么办。
请帮助。
https://docs.python.org/3/library/functions.html#int
If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2–36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int('010', 0) is not legal, while int('010') is, as well as int('010', 8).
当您将输入字符串传递给内置 int
函数时,它拒绝输入字符串中的小数点。
num = "1.5"
print(type(num)) # <class 'str'>
print(int(num)) # ValueError: invalid literal for int() with base 10: '1.5'
num = "2"
print(type(num)) # <class 'str'>
print(int(num)) #2
num = 1.5
print(type(num)) # <class 'float'>
int_num = int(num)
print(type(int_num)) # <class 'int'>
print(int_num) # 1
num = "1.5"
float_num = float(num)
print(type(float_num)) # <class 'float'>
print(float_num) # 1.5
int_num = int(float_num)
print(type(int_num)) # <class 'int'>
print(int_num) # 1
这个错误是因为我们试图将“100.5: 转换为整数。值“100.5”被格式化为字符串。 Python 无法将字符串中的浮点数转换为整数。
我们可以使用 float() 和 int() 语句来做到这一点。 int() 函数 returns 一个整数。 float() 函数 returns float 的浮点表示。
解法:
将第 41 行替换为 output4 = int(float(input1))
尝试将字符串浮点数转换为整数时发生错误。
为了避免这个错误,必须先把字符串转成浮点数,然后再转成整数。
一个简单的代码解决方案是在 int 方法中添加一个 float 方法。只需将第 41 行更改为:
output4 = int(float(input1))
我是 python 的新手。我英语不好。 我enter image description here 得到一个错误“以 10 为底的 int() 的无效文字” 我将一个浮点数从 float 转换为 int 并得到了那个错误。 我不知道该怎么办。 请帮助。
https://docs.python.org/3/library/functions.html#int
If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2–36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int('010', 0) is not legal, while int('010') is, as well as int('010', 8).
当您将输入字符串传递给内置 int
函数时,它拒绝输入字符串中的小数点。
num = "1.5"
print(type(num)) # <class 'str'>
print(int(num)) # ValueError: invalid literal for int() with base 10: '1.5'
num = "2"
print(type(num)) # <class 'str'>
print(int(num)) #2
num = 1.5
print(type(num)) # <class 'float'>
int_num = int(num)
print(type(int_num)) # <class 'int'>
print(int_num) # 1
num = "1.5"
float_num = float(num)
print(type(float_num)) # <class 'float'>
print(float_num) # 1.5
int_num = int(float_num)
print(type(int_num)) # <class 'int'>
print(int_num) # 1
这个错误是因为我们试图将“100.5: 转换为整数。值“100.5”被格式化为字符串。 Python 无法将字符串中的浮点数转换为整数。
我们可以使用 float() 和 int() 语句来做到这一点。 int() 函数 returns 一个整数。 float() 函数 returns float 的浮点表示。
解法:
将第 41 行替换为 output4 = int(float(input1))
尝试将字符串浮点数转换为整数时发生错误。 为了避免这个错误,必须先把字符串转成浮点数,然后再转成整数。
一个简单的代码解决方案是在 int 方法中添加一个 float 方法。只需将第 41 行更改为:
output4 = int(float(input1))