Python3 error message: ' Float ' object cannot is interpreted as an integer

Source: Internet
Author: User
Tags win32

background:



Bmpixel

194***4370@qq.com

List reversal function:

#!/user/bin/python
#-*-coding:utf-8-*-

def reverse (LI):
    for I in range (0, Len (LI)/2):
        temp = Li[i]
  li[i] = li[-i-1]
        li[-i-1] = temp

L = [1, 2, 3, 4, 5]
reverse (L)
print (L)
Error message in Python3: ' Float ' object cannot be interpreted as an integer, and in Python2 normal operation;


Problem Solving

Why is the data in the range float?

There is a difference between py2 and py3, and there is a difference in the search. Please see the following usage:

Pyhon2

C:\users\chenjun>python
Python 2.7.12 (v2.7.12:d33e0cf91556, June 2016, 15:24:40) [MSC v.1500-bit (
AMD64)] on Win32
Type ' help ', ' copyright ', ' credits ' or ' license ' for the more information.
>>> 0/200 + 1
1
>>> 1/200 + 1
1

Python3

C:\users\chenjun>python3
Python 3.6.1 (V3.6.1:69C0DB5, Mar 2017, 17:54:52) [MSC v.1900 bit (Intel)]
On Win32
Type ' help ', ' copyright ', ' credits ' or ' license ' for the more information.
>>> 0/200 + 1
1.0
>>> 1/200 + 1
1.005

In the python2,/only left the integer part, minus the decimal, is int type. In Python3, the result is division in real sense, and the result is float type. So there is the error message: ' Float ' object cannot is interpreted as an integer.


Workaround: Convert a float type to an int type









Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.