Python Forum
TypeError: list indices must be integers or slices, not float
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: list indices must be integers or slices, not float
#1
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
Reply
#2
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))
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 349 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  tuple indices must be integers or slices, not str cybertooth 16 11,698 Nov-02-2023, 01:20 PM
Last Post: brewer32
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,215 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,297 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,373 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,468 Mar-24-2023, 08:34 AM
Last Post: fullytotal
Question How to append integers from file to list? Milan 8 1,464 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  convert string to float in list jacklee26 6 1,951 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,065 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable #1 isdito2001 1 1,105 Jan-21-2023, 12:43 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020