ValueError: invalid literal for int() with base 10: '7.8000000e+02'

来源:互联网 发布:去马赛克软件下载 编辑:程序博客网 时间:2024/06/08 17:00

ValueError: invalid literal for int() with base 10: '7.8000000e+02'

1. ValueError 1

number_string = "7.8000000e+02"number_value = int(number_string)

==>

/usr/bin/python2.7 /home/strong/PycharmProjects/crash_course/python_function/number_string.pyTraceback (most recent call last):  File "/home/strong/PycharmProjects/crash_course/python_function/number_string.py", line 4, in <module>    number_value = int(number_string)ValueError: invalid literal for int() with base 10: '7.8000000e+02'Process finished with exit code 1

Python int( ) 函数的输入字符串中可以包含数字字符和 + 、-符号,不能包含其它非数字字符。


2. example 1

number_string = "7.8000000e+02"float_value = float(number_string)int_value1 = int(float_value)int_value2 = int(float(number_string))print("float_value: " + str(float_value))print("int_value1: " + str(int_value1))print("int_value2: " + str(int_value2))

==>

/usr/bin/python2.7 /home/strong/PycharmProjects/crash_course/python_function/number_string.pyfloat_value: 780.0int_value1: 780int_value2: 780Process finished with exit code 0

3. example 2

number_string1 = "+99"number_string2 = "-99"int_value1 = int(number_string1)int_value2 = int(number_string2)print("int_value1: " + str(int_value1))print("int_value2: " + str(int_value2))

==>


/usr/bin/python2.7 /home/strong/PycharmProjects/crash_course/python_function/number_string.pyint_value1: 99int_value2: -99Process finished with exit code 0







阅读全文
0 0
原创粉丝点击