Ask Your Question

# 'list indices must be integers, not tuple' error in newbie script

Hi expers!!

Im a new user of SAGE. Right now im writing a script that plot 'N' random lines of lenght=1 in a rectangle of b*h center in axes. The code is shhow below:

N=float(raw_input('Número de rectas aleatóreos (N)?:\n'))
b=float(raw_input('Base del rectángulo (b>1)?:\n'))
h=float(raw_input('Altura del cuadrado (h>1)?:\n'))

uniforme_x=RealDistribution('uniform',[-b/2,b/2])
uniforme_y=RealDistribution('uniform',[-h/2,h/2])

lista_x=[uniforme_x.get_random_element() for j in srange(N)]
lista_y=[uniforme_y.get_random_element() for j in srange(N)]

lista_xy = zip(lista_x , lista_y)

lineas=[]

d2=1*1*2

for j in lista_xy:
x1 = lista_xy[j][0]
y1 = lista_xy[j][1]
while d2>1*1:
x2 = uniforme_x.get_random_element()
y2 = uniforme_x.get_random_element()
d2=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)
lineas.append(line([lista_xy[j],(x2,y2)]))

grafico=sum(lineas)

grafico.show()

When i execute this code I get the error messege 'list indices must be integers, not tuple'.

Whats the problem?

Waiting for your answers.

Thanks a lot!

edit retag close merge delete

## 2 answers

Sort by » oldest newest most voted

This is a Python newbie error, not really a Sage error. Here is some sample code, where I've inputted 5, 4, and 3 for your raw_input things.

sage: lista_xy
[(0.790543686599, 0.496880250983),
(-0.00532463751733, -0.648275931599),
(1.5688782027, 0.968919148436),
(-1.65441823658, 0.556830151705),
(-0.270724125206, 1.49786789808)]

Note that each j in this lista is a tuple of floating point numbers. So you can't take the [j] index. If you remove the [j] pieces and use j itself, your code works fine:

for j in lista_xy:
x1 = j[0]
y1 = j[1]
while d2>1*1:
x2 = uniforme_x.get_random_element()
y2 = uniforme_x.get_random_element()
d2=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)
lineas.append(line([j,(x2,y2)]))

Good luck!

more

Thanks a lot. Im a Python newbie too.

Best regards,

more

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2013-06-05 15:41:32 -0500

Seen: 11,785 times

Last updated: Jun 06 '13