📌  相关文章
📜  ValueError: int() 以 10 为底的无效文字:site:stackoverflow.com - Python (1)

📅  最后修改于: 2023-12-03 15:35:33.618000             🧑  作者: Mango

Introduction to ValueError: int() invalid literal for int() with base 10 error

When working with Python, you might encounter a ValueError with the message "int() invalid literal for int() with base 10". This error occurs when you try to convert a string to an integer using the int() function and the string is not a valid integer literal.

Common causes of this error

There are several common scenarios that can trigger this error:

  • Trying to convert a string that contains non-numeric characters to an integer
  • Trying to convert a floating-point number to an integer
  • Trying to convert a string with a decimal point to an integer
  • Trying to convert a string in a format other than base-10 to an integer

Here are some examples of code that can trigger this error:

# Example 1: Non-numeric characters in string
string = "hello"
number = int(string) # Raises ValueError

# Example 2: Floating-point number
string = "3.14"
number = int(string) # Raises ValueError

# Example 3: String with decimal point
string = "42.0"
number = int(string) # Raises ValueError

# Example 4: String in non-base-10 format
string = "1010"
number = int(string, 2) # Raises ValueError
How to handle this error

When you encounter a ValueError with the message "int() invalid literal for int() with base 10", you can handle it by doing the following:

  1. Check the variable that you are trying to convert to an integer and make sure that it is in the correct format (i.e., contains only numeric characters and no decimal points).
  2. If the variable contains non-numeric characters, you can remove them using a regex or by using the replace() method.
  3. If the variable contains a decimal point, you can either use the float() function to convert it to a floating-point number or round it to the nearest integer using the round() function before converting it to an integer.
  4. If you are trying to convert a string in a non-base-10 format, you need to specify the correct base as the second argument to the int() function.

Here is an example code that demonstrates how to handle this error:

string = "42.0"
if '.' in string:
    number = int(float(string))
else:
    number = int(string)

In this code, we first check if the variable string contains a decimal point. If it does, we convert it to a floating-point number using the float() function and then convert it to an integer using the int() function. If string does not contain a decimal point, we simply convert it to an integer using the int() function.

Conclusion

The ValueError with the message "int() invalid literal for int() with base 10" can occur when you try to convert a string to an integer and the string is not in the correct format. You can handle this error by checking the variable that you are trying to convert and making sure that it is in the correct format, removing non-numeric characters if necessary, converting floating-point numbers to integers using the round() or float() function, and specifying the correct base if you are converting a string in a non-base-10 format.