ValueError: invalid literal for int() with base 10: '10.1'


自定義輸入時,提示ValueError: invalid literal for int() with base 10: '10.1'錯誤

>>> a = int(raw_input('please input something:'))
please input something:10.1


Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '10.1'

 

原因是數字字符串不能直接轉int類型,需要轉為float類型后才能轉int類型:

>>> int(float("10.1"))
10

或者:

>>> a = float(raw_input('please input something:'))
please input something:10.1
>>> type(a)
<type 'float'>
>>> int(a)
10


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM