Ask Your Question
0

for-while loop problem

asked 2013-06-06 13:11:30 +0100

mresimulator gravatar image

updated 2013-06-06 14:22:40 +0100

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'))
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 = 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.

Waiting for your answers.

Best regards.

edit retag flag offensive close merge delete

Comments

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

calc314 gravatar imagecalc314 ( 2013-06-06 13:19:01 +0100 )edit

4 Answers

Sort by » oldest newest most voted
0

answered 2013-06-06 14:50:10 +0100

tmonteil gravatar image

updated 2013-06-06 14:51:48 +0100

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.

edit flag offensive delete link more
0

answered 2013-06-06 14:20:37 +0100

tmonteil gravatar image

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.

edit flag offensive delete link more
0

answered 2013-06-06 14:26:38 +0100

mresimulator gravatar image

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.

Waiting for your answers.

Best regards.

edit flag offensive delete link more
0

answered 2013-06-06 17:29:15 +0100

mresimulator gravatar image

Perfect!!!

Thaks so much tmonteil!

Best regards.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2013-06-06 13:11:30 +0100

Seen: 2,710 times

Last updated: Jun 06 '13