The error TypeError: 'int' object is not subscriptable
appears because at some point you try to apply indexes ( subscripts ) to a variable that is of type int
.
Indeed, the expression n[i]
uses subscripts on the variable n
, but this is not a list, but an integer as we see in n = 6
.
The same thing happens with the variables l
and L
that you initialize with numeric values (in the case of L
is a float
), but later on you try to access with l[i]
and L[i]
.
Update
Once Maria has provided the statement of the exercise she was trying to solve, I have a clearer idea of how to approach it and I can suggest improvements to the code, such as the following:
It is not necessary to use lists or arrays, unless you want to go accumulating in a list what is going out for the different values of n
, but it does not seem that they ask for that, so you can limit yourself to print by display the results without saving them in lists.
Apparently therefore it is enough to eliminate the [i]
of all parts in your program.
The line that recalculates l
within the for
does not seem to correspond to the one given in the statement, which said l'=sqrt(L'+l/2)
. You use a product instead of a sum and you need to use l
as part of the expression.
The return
of the last line would be left over, otherwise the function would come out in the first iteration of the loop (in addition to returning what returns print
does not make sense, since print
always returns None
)
One last detail, the import math
should be outside the function.
With these suggestions your code would be:
import math
def arquimides(x):
n = 6
l = 1
L = 2/math.sqrt(3)
per = n*l/2
PER = n*L/2
print(17*' ', 'n', 17*' ', 'l',17*' ', 'L',17*' ', 'per',17*' ', 'PER')
print(17*' ', n, 17*' ', l, 17*' ', L, 17*' ', per, 17*' ', PER)
for i in range (x):
n = 2*n
L = l*L/(l + L)
l= math.sqrt(L+l/2)
per = n*l/2
PER = n*L/2
print(10*' ', n, 10*' ', l, 10*' ', L, 10*' ', per, 10*' ', PER)
Which already can be executed without errors, but produces a result that I fear is not correct. This would be the output of the program (for x=5
):
n l L per PER
6 1 1.1547005383792517 3.0 3.4641016151377553
12 1.0177909337689375 0.5358983848622455 6.106745602613625 3.215390309173473
24 0.9273358762479919 0.3510563604921621 11.128030514975903 4.212676325905945
48 0.8475385044421772 0.2546535783880865 20.340924106612253 6.1116858813140755
96 0.7871384548501504 0.1958176948827938 37.78264583280722 9.399249354374103
192 0.741874303108312 0.15680825418736846 71.21993309839796 15.053592401987373
Apart from the fact that the columns are not well aligned (which could be fixed using appropriate format strings), the values are what worry me since, if I did not misunderstand, what Archimedes was looking for was an approximation to the number π , which would be so much the better the higher n
. Instead the numbers that come out of you grow instead of approaching π.
Surely some of the formulas used is not copied correctly, or missing parentheses in a numerator or denominator, or something like that. But I do not know the exact formulation that Archimedes made of this problem, so I can not tell you exactly where the error is.