2

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!

5
  • Why pi[digit] ? Commented Oct 29, 2017 at 1:37
  • You have defined pi as a number. You cannot index into a number (no square brackets). Commented Oct 29, 2017 at 1:46
  • James, I was wanting o index into it, but apparently I can but just not into a floating point Commented Oct 29, 2017 at 12:11
  • In python you can index into strings, lists, and tuples. But only list has item assignment. Commented Oct 29, 2017 at 16:55
  • 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 Commented Oct 29, 2017 at 18:30

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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...
0

A "subscript" refers to the use of [] for indexing. A float is not indexable, because it's not a sequence or container. You can use str(pi)[digit] although that would include the decimal point.

3 Comments

This should be str(pi)[digit].
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
That's the limit to how precise a float can be, so there's not much else you can do...
0
import math
from math import pi as pi #use pythons math pi or you can add your own
divideBy = 1
digit = 1
digitChar = 3
digitOccurance = 1
PI = str(pi) # convert float pi to string so you can do your calculations and use the str
while True:
    pi = pi - 4 / divideBy
    divideBy = divideBy + 2
    pi = pi + 4 / divideBy
    divideBy = divideBy + 2
    try: 
        if PI[digit] == digitChar:
            digit = digit + 1
            digitOccurance = 0
            print(digitChar)
        else:
            digitChar = PI[digit]

    except IndexError: # stop the index error that is produced
        break

3 Comments

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
where is your methodology coming from?
en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.