python,字符串浮点型转整型问题

python,字符串浮点型转整型问题

  • ValueError: invalid literal for int() with base 10
    • 错误原因

ValueError: invalid literal for int() with base 10

复原错误:

str_a = '1.5'
int_a = int(str_a)

修正错误:

# 修正方式1
str_a = '1.5'
int_a = int(float(str_a))

# 修正方式2
str_a = '1.5'
int_a = int(eval(str_a))

错误原因

浮点型字符串无法直接转int类型

你可能感兴趣的:(python基础,python)