(Python) TypeError: 'float' object is not subscriptable

37,869

For floats (and ints) you cannot simply access the nth character. That's what "is not subscriptable" means.

Strings on the other hand are subscriptable (as are lists and dictionaries). So the very basic workaround is to convert your float to a string. The new problem though is that the "." in the float is a part of the string. So when doing your str(pi)[digit] == digitChar you have to make sure that the types align. You could do int(str(pi)[digit]) == digitChar, but this would fail for the "." character. Or, you can do str(pi)[digit] == str(digitChar) and that would work for everything.

You could also set it up so that digitChar is always a string. Since you've called it digitCar, it would make sense that the type of the variable is also a string.

pi = 0
divideBy = 1
digit = 1
digitChar = '3'
digitOccurance = 1
while True:
    pi = pi - 4 / divideBy
    divideBy = divideBy + 2
    pi = pi + 4 / divideBy
    divideBy = divideBy + 2

    # digitChar is a string
    if str(pi)[digit] == digitChar:
        digit = digit + 1
        digitOccurance = 0
        print(digitChar)
    else:
        # make sure to maintain `digitChar` as a string/char type
        digitChar = str(pi)[digit]

As for the output, it looks like you have some errors in your logic that are preventing you from getting the right output.

Share:
37,869

Related videos on Youtube

sirfused
Author by

sirfused

I am Cameron and I am a new member of the programming family. I just started learning code and I am using this site to help me. I make fun games right now, but I am hoping to proper improve my computer with my own code (for example, send me a noise when I receive a notification on stack overflow on the official and new notifications bar) Thanks for all the help

Updated on October 30, 2020

Comments

  • sirfused
    sirfused over 3 years

    I have finally got the patience to use Python again and I am trying some math stuff to get back in, but I am now trying to make a Python script and it came up with the "float' object is not subscriptable' error. Again, I have just got back into Python and I have no clue what this means. It may be obvious, but all I am seeing on stackoverflow is people telling others how to fix their specific code rather than explaining what it means so I don't know what could be causing it. All I know is that it has something to do with the fact Pi is a decimal number (floating point number??) but I don't see why it shouldn't be able to get nth character of it.

    It comes up for "if pi[digit] == digitChar:" on line 11. Here is my code:

    pi = 0
    divideBy = 1
    digit = 1
    digitChar = 3
    digitOccurance = 1
    while True:
        pi = pi - 4 / divideBy
        divideBy = divideBy + 2
        pi = pi + 4 / divideBy
        divideBy = divideBy + 2
        if pi[digit] == digitChar:
            digit = digit + 1
            digitOccurance = 0
            print(digitChar)
        else:
            digitChar = pi[digit]
    

    The last bit of code is basically to stop the spam and to just show one digit at a time. It is meant to get the [digit] digit of 'pi' and test if it was the same as the previous one. If it is, it moves along a character and prints the Character [digitChar].

    Thanks so much for the help in advance!

    • scharette
      scharette over 6 years
      Why pi[digit] ?
    • James
      James over 6 years
      You have defined pi as a number. You cannot index into a number (no square brackets).
    • sirfused
      sirfused over 6 years
      James, I was wanting o index into it, but apparently I can but just not into a floating point
    • Gumboy
      Gumboy over 6 years
      In python you can index into strings, lists, and tuples. But only list has item assignment.
    • sirfused
      sirfused over 6 years
      IK, but I don't want to remove the full stop and when I turn it into a string, it comes up with an error plusthe whole number is incorrect anyways
  • Matthias Fripp
    Matthias Fripp over 6 years
    This should be str(pi)[digit].
  • sirfused
    sirfused over 6 years
    This did not fix the issue. This made it instantly bring out a number and then after 15 digits (including decimal point) it came up with a string error 'if str(pi)[digit] == digitChar: IndexError: string index out of range' bringing out 2 . 0 9 4 0 0 6 0 8 7 0 2 4 7
  • sirfused
    sirfused over 6 years
    I actually wanted the '.' to be included and again it just instantly went with 2 0 9 4 0 0 6 0 8 7 0 2 4 7 rather than 3.14159...
  • sirfused
    sirfused over 6 years
    I don't want to just import pi. The whole point is to calculate it, and the way you explained it using comments didn't help too much. It came up with the same incorrect number, so I will try adding 1 to the digitOccurance
  • Gumboy
    Gumboy over 6 years
    where is your methodology coming from?
  • luther
    luther over 6 years
    That's the limit to how precise a float can be, so there's not much else you can do...
  • sirfused
    sirfused over 6 years
    en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80