天天看点

Python报错集合篇1-ValueError: invalid literal for int() with base 10: 'asd'

本文介绍如何处理报错–ValueError: invalid literal for int() with base 10: 'asd’

源代码:

#列如:
print(int(‘asd’))
print(int(‘1.11’))
print(int(‘-1.11’))
print(int(‘你好’))

           

此类运行都会报错如下:

Traceback (most recent call last):
  File "D:/Program Files/PycharmProjects/pycharmPrj/zzz/Python/lx0.py", line 46, in <module>
    print(int('asd'))
ValueError: invalid literal for int() with base 10: 'asd'

           

由于‘asd’无法使用int()转化为整数,因此提示‘asd’值无效。只能int()整数构成str值

例如以下代码:

print(int(‘-10’))
print(int(‘10’))

           

执行如下:

C:\Python37\python.exe "D:/Program Files/PycharmProjects/pycharmPrj/zzz/Python/lx0.py"
-10
10
Process finished with exit code 0