Difficulties with the FOR command in Python

Asked

Viewed 127 times

0

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

I would like the program to work for other ". txt" files as well (of course, provided this file provides the necessary instructions). I’ve been told that I could automate this part of the code using a loop like for, but it didn’t work out at all. Someone can 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()
  • What is the file format desenho.txt? And what would be the formats of the other files?

2 answers

2


Analyzing 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, then we can do the for in ang and accumulate; It stays like this:

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

That way it will work no matter the size of ang

0

I was able to solve the problem of repetition of azmt using a loop with two conditionals inside, as put below. Thank you all for your 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()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.