It looks like you are using Python 3.x. One of the important differences in Python 3.x is the way division is handled. When you do x / y
, an integer is returned in Python 2.x because the decimal is truncated (floor division). However in 3.x, the /
operator performs ‘true’ division, resulting in a float
instead of an integer (e.g. 1 / 2 = 0.5
). What this means is that your are now trying to use a float to reference a position in a list (e.g. my_list[0.5]
or even my_list[1.0]
), which will not work as Python is expecting an integer. Therefore you may first want to try using middle = (first + last) // 2
, adjusting so that the result returns what you expect. The //
indicates floor division in Python 3.x.
Related Posts:
- TypeError: list indices must be integers or slices, not list
- TypeError: slice indices must be integers or None or have an __index__ method
- Python cant convert ‘list’ object to str error [closed]
- Python – TypeError: ‘int’ object is not iterable
- Python – TypeError: ‘int’ object is not iterable
- How to overcome TypeError: unhashable type: ‘list’
- not all arguments converted during string formatting.. NO % variables
- What does the list() function do in Python?
- TypeError: not all arguments converted during string formatting python
- Relative imports in Python 3
- ModuleNotFoundError: No module named ‘requests’. But ‘requests’ already installed
- What are type hints in Python 3.5?
- ImportError: No module named ‘keras’
- TypeError: ‘list’ object cannot be interpreted as an integer
- ImportError: libcublas.so.9.0: cannot open shared object file
- How to install xlrd in python3 library
- How to import cv2 in python3?
- ‘python3’ is not recognized as an internal or external command, operable program or batch file
- How to find all the indexes of a recurring item in a list?
- Unable to initialize device PRN in Python
- Error when executing `jupyter notebook` (No such file or directory)
- How to resolve TypeError: ‘No Numeric Data to Plot’, when numeric data exists in Python
- python3 is not recognized as an internal or external command, operable program or batch file showing for creating django runserver
- python3 : The term ‘python3’ is not recognized as the name of a cmdlet
- How can I solve ” module ‘pandas’ has no attribute ‘scatter_matrix’ ” error?
- Getting a map() to return a list in Python 3.x
- Install numpy on python3.3 – Install pip for python3
- How to import cv2 in python3?
- Why did ‘reset_index(drop=True)’ function unwantedly remove column?
- TypeError: unsupported operand type(s) for -: ‘list’ and ‘list’
- Python: create a pandas data frame from a list
- Python 3 – ValueError: not enough values to unpack (expected 3, got 2)
- How to return dictionary keys as a list in Python?
- TypeError: ‘_io.TextIOWrapper’ object is not subscriptable
- python 3 – x for x for loops – how do they work?
- Visual Studio Python “Failed to launch the Python Process, please validate the path ‘python” & Error: spawn python ENOENT
- TypeError: module.__init__() takes at most 2 arguments (3 given)
- WinError 10061 – No Connection Could be made
- how do I upgrade pip on Mac?
- SyntaxError: unexpected EOF while parsing
- How can I reverse a list in Python?
- Difference between del, remove, and pop on lists
- How can I reverse a list in Python?
- How to use filter, map, and reduce in Python 3
- converting time from UTC to CST
- Use Gif Logo For Loading Screen In Kivy
- Praw & Discord.py: The bot keep sending the same meme. I want the bot to send different meme whenever it is asked
- What is the difference between Python’s list methods append and extend?
- How can I make a time delay in Python? [duplicate]
- Understanding slice notation
- Understanding the main method of python [duplicate]
- Understanding slice notation
- TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- How to open a file using the open with statement
- TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- Microsoft Visual C++ 14.0 is required (Unable to find vcvarsall.bat)
- TypeError: list indices must be integers or slices, not str
- How to install pip with Python 3?
- What is the difference between rw+ and r+
- Convert bytes to a string
- IndexError: list index out of range and python
- How can I represent an ‘Enum’ in Python?
- Import Error: No module named numpy
- Python3 – ModuleNotFoundError: No module named ‘numpy’
- Relative imports – ModuleNotFoundError: No module named x
- Python: ‘ModuleNotFoundError’ when trying to import module from imported package
- Relative imports – ModuleNotFoundError: No module named x
- How to convert list to string [duplicate]
- TypeError: a bytes-like object is required, not ‘str’ when writing to a file in Python3
- How to uninstall a package installed with pip install –user
- How do I get the number of elements in a list?
- How do I upgrade the Python installation in Windows 10?
- Difference between del, remove, and pop on lists
- How to avoid “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it? [duplicate]
- TypeError: ‘builtin_function_or_method’ object is not subscriptable
- How do I use raw_input in Python 3
- Python Variable Declaration
- Relative imports in Python 3
- Relative imports in Python 3
- Finding the index of an item in a list
- Difference between Arrays.asList(array) and new ArrayList
(Arrays.asList(array)) - how to calculate binary search complexity
- Finding the index of an item in a list
- Accessing the index in ‘for’ loops?
- working of \n in python [duplicate]
- Pip freeze vs. pip list
- How to make a flat list out of a list of lists
- Convert list to array in Java [duplicate]
- Removing duplicates in lists
- Accessing the index in ‘for’ loops?
- Best way to convert string to bytes in Python 3?
- Best way to convert string to bytes in Python 3?
- List changes unexpectedly after assignment. Why is this and how can I prevent it?
- Why does this iterative list-growing code give IndexError: list assignment index out of range?
- PermissionError: [Errno 13] Permission denied
- Binary Search Tree Implementation in C++ STL?
- What is setup.py?
- How is Anaconda related to Python?
- PermissionError: [Errno 13] in Python
- FileNotFoundError: [WinError 2] The system cannot find the file specified: