(Python) TypeError: 'float' object is not subscriptable
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.
Related videos on Youtube
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, 2020Comments
-
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 over 6 yearsWhy
pi[digit]
? -
James over 6 yearsYou have defined
pi
as a number. You cannot index into a number (no square brackets). -
sirfused over 6 yearsJames, I was wanting o index into it, but apparently I can but just not into a floating point
-
Gumboy over 6 yearsIn python you can index into strings, lists, and tuples. But only list has item assignment.
-
sirfused over 6 yearsIK, 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 over 6 yearsThis should be
str(pi)[digit]
. -
sirfused over 6 yearsThis 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 over 6 yearsI 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 over 6 yearsI 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 over 6 yearswhere is your methodology coming from?
-
luther over 6 yearsThat's the limit to how precise a
float
can be, so there's not much else you can do... -
sirfused over 6 yearsen.wikipedia.org/wiki/Leibniz_formula_for_%CF%80