Python Forum
TypeError: list indices must be integers or slices, not float - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: TypeError: list indices must be integers or slices, not float (/thread-8613.html)



TypeError: list indices must be integers or slices, not float - RedSkeleton007 - Feb-28-2018

Why can't I just get two decimal places to the right of the decimal point?
Or are the circumstances with list indexes different:
#!/usr/bin/env python3
#SamsLists.py

myInts = [1,8,4,7,6]
for i in myInts:
    print(i)

moneyFloats = [2.1, 6.2, 7.8, 9.1, 5.4]
for i in moneyFloats:
    print("{:,.2f}".format(moneyFloats[i]))
Error:
Traceback (most recent call last): File "I:/Python/Python36-32/SamsPrograms/SamsLists.py", line 10, in <module> print("{:,.2f}".format(moneyFloats[i], 2)) TypeError: list indices must be integers or slices, not float



RE: TypeError: list indices must be integers or slices, not float - wavic - Feb-28-2018

moneyFloats contains floats.
Just remove the index part in format method.

moneyFloats = [2.1, 6.2, 7.8, 9.1, 5.4]
for num in moneyFloats:
    print("{:,.2f}".format(num))