ValueError: invalid literal for int () with base 10

前端 未结 3 607
孤城傲影
孤城傲影 2020-11-28 05:29

I wrote a program to solve y = a^x and then project it on a graph. The problem is that whenever a < 1 I get the error:

V

3条回答
  •  执念已碎
    2020-11-28 06:29

    As Lattyware said, there is a difference between Python2 & Python3 that leads to this error:

    With Python2, int(str(5/2)) gives you 2. With Python3, the same gives you: ValueError: invalid literal for int() with base 10: '2.5'

    If you need to convert some string that could contain float instead of int, you should always use the following ugly formula:

    int(float(myStr))
    

    As float('3.0') and float('3') give you 3.0, but int('3.0') gives you the error.

提交回复
热议问题