# for-while loop problem

Hi experts!

Im a newbie Python and sage user.

Im writing a script for draw N random lines in a rectangle of b*h center in axes.

The actual code is:

print('Distribuye N rectas aleatorios de longitud 1 en un rectángulo de b x h centrado en el origen de un par de ejes cartesianos\n')

N=float(raw_input('Número de rectas aleatóreos (N)?:\n'))
b=float(raw_input('Base del rectángulo (b>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 = 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([j,(x2,y2)])

grafico=sum(lineas)

grafico.show()


The associated output is a graph of N lines. Each line comes from a different item from the list zip BUT end at the same point (x2, y2). Furthermore the lines have length (d2) greater than 1.

I cant find the error. I know this is a basic question but i hope you can help me.

Best regards.

edit retag close merge delete

Looks to me like you have an issue with brackets and parentheses in the line lineas.append(j,(x2,y2)]))

( 2013-06-06 06:19:01 -0500 )edit

Sort by » oldest newest most voted

Perfect!!!

Thaks so much tmonteil!

Best regards.

more

In your while d2>1*1: loop, d2 is a global variable, so after the first round, d2 becomes less than 1. Then this loop is never run again since the condition d2>1*1 is always False. This explains why (x2, y2) is always the same.

What you should do it to reset d2 at each pass of the for loop:

d2 = 2
while d2>1*1:


By the way, replacing 1*1 by 1 won't change anything and will lead to a cleaner code.

more

Sorry, i posted a old version code. I execute the corrected code: the associated output is a graph of N lines. Each line comes from a different item from the list zip BUT end at the same point (x2, y2). Furthermore the lines have length (d2) greater than 1.

I cant find the error. I know this is a basic question but i hope you can help me.

Best regards.

more

After 5 questions, 4 answers and 43 point of karma, you are assumed to become autonomous with syntax errors ;)

If i put your code in a Sage session i get:

File "<ipython-input-7-54c6265f464d>", line 19
for j lista_xy:
^
SyntaxError: invalid syntax


So, it means that your loop is not well defined, you should replace it by:

for j in lista_xy:


If i do this again, i got:

 File "<ipython-input-9-bf0cc464ad8d>", line 26
lineas.append(j,(x2,y2)]))
^
SyntaxError: invalid syntax


Which is the problem @calc314 mentionned. As the name lineas suggests, you seem to build a list of lines, i guess that you meant:

lineas.append(line([j,(x2,y2)]))


Then your program seems to work. At least there are no syntax errors left, hence you can try to see whether the output looks like what you are looking for.

more