difference between python2 and python3 - int() and input()

前端 未结 2 2082
别那么骄傲
别那么骄傲 2021-01-18 10:23

I wrote below python code. And I found that python2 and python3 has totally difference running result for input of 1.1. Why is there such difference between python2 and pyth

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-18 11:17

    Please check the Python 3 release notes. In particular, the input() function (which is considered dangerous) was removed. In its stead, the safer raw_input() function was renamed to input().

    In order to write code for both versions, only rely on raw_input(). Add the following to the top of your file:

    try:
        # replace unsafe input() function
        input = raw_input
    except NameError:
        # raw_input doesn't exist, the code is probably
        # running on Python 3 where input() is safe
        pass
    

    BTW: Your example code isn't minimal. If you had further reduced the code, you would have found that in one case int() operates on a float and in the other on a str which would then have brought you to the different things that input() returns. A look at the docs would then have given you the final hint.

提交回复
热议问题