Difficulties with FOR command in Python [closed]

0

I'm putting together a program that reads information about angles and distances from a text file and draws it according to the information contained therein. The program should also calculate the azimuths, and that's what I did, these variables that start with azmt represent the azimuth of each side of the polygon, but I did not do it the smarter way, because the code only runs to this specific file ("drawing.txt").

I would like the program to work for other ".txt" files as well (of course, as long as this file provides the necessary instructions). I've already been told that I could automate this part of the code using a for loop, but it did not work out at all. Can someone give me a suggestion?

import turtle
f = open('desenho.txt')
turtle.speed(1)
turtle.mode("logo")
ang = []
dist = []
azmt = []
for line in f:
    a = line.split(" ")
    ang = ang + [float(a[0])]
    dist = dist + [float(a[1])]
n = len(dist)
azmt0 = ang[0]
azmt1 = azmt0 + ang[1]
azmt2 = azmt1 + ang[2]
azmt3 = azmt2 + ang[3]
azmt4 = azmt3 + ang[4]
azmt5 = azmt4 + ang[5]
azmt6 = azmt5 + ang[6]
azmt7 = azmt6 + ang[7]
azmt8 = azmt7 + ang[8]
azmt9 = azmt8 + ang[9]
azmt10 = azmt9 + ang[10]
azmt11 = azmt10 + ang[11]
azmt = [float(azmt0)]+[float(azmt1)]+[float(azmt2)]+[float(azmt3)]+[float(azmt4)]+[float(azmt5)]+[float(azmt6)]+[float(azmt7)]+[float(azmt8)]+[float(azmt9)]+[float(azmt10)]+[float(azmt11)]
print(azmt)
for i in range(n):
    turtle.rt(ang[i])
    turtle.fd(dist[i])
turtle.done()
    
asked by anonymous 21.12.2018 / 14:39

2 answers

2

Looking at this part:

azmt0 = ang[0]
azmt1 = azmt0 + ang[1]
azmt2 = azmt1 + ang[2]
...
azmt11 = azmt10 + ang[11]

Each azmtXX is actually the value of ang accumulated, so we can do for in ang and accumulate; It looks like this:

azmt = []
ang_acumulado = 0
for um_ang in ang:
    ang_acumulado += um_ang
    azmt.append(ang_acumulado)

This will work no matter what the size of ang

    
21.12.2018 / 18:36
0

I was able to solve the problem of azmt repetition using a loop with two conditionals inside, as shown below. Thank you all for the help.

import turtle
from math import radians, sin, cos
f = open('simples.txt')
turtle.speed(1)
turtle.mode("logo")
ang = []
dist = []
azmt = []
seno = []
cosseno = []
x = []
y = []
for line in f:
    a = line.split(" ")
    ang = ang + [float(a[0])]
    dist = dist + [float(a[1])]
n = len(dist)
for i in range(n):
    if i == 0:
        azmt.append(ang[i])
    else:
        azmt.append(azmt[i-1] + ang[i])
print("Vetor contendo os azimutes:")
print(azmt)
for i in range(n):
    seno.append(round(sin(radians(azmt[i])),7))
    cosseno.append(round(cos(radians(azmt[i])),7))
for i in range(n):
    x.append(round((dist[i] * seno[i]),4))
    y.append(round((dist[i] * cosseno[i]),4))
print("Vetor contendo as coordenadas em x:")
print(x)
print("Vetor contendo as coordenadas em y:")
print(y)
xmed = (max(x) + min(x)) / 2
ymed = (max(y) + min(y)) / 2
turtle.pu()
turtle.goto(xmed,ymed)
turtle.pd()
for i in range(n):
    turtle.rt(ang[i])
    turtle.fd(dist[i])
turtle.done()
    
24.12.2018 / 02:16