0

I am trying to make some code that finds the coordinates of a satellite in orbit, but I am currently trying to do it for just a circle where you ignore gravity etc. and you are already given the radius. I have already tried this code but the loop wont work please help

import math

r = 5

angle = 0

while angle <= 360:

    angle = math.radians(angle)

    x_coord = math.sin(angle)*r

    y_coord = math.cos(angle)*r
    print ("Position [",x_coord,y_coord,"]")
    angle +=1
1
  • 1
    Although an answer has already pointed out the problem here, in future please specify what you mean by "won't work" in the context of a piece of code. Commented Nov 26, 2018 at 16:55

2 Answers 2

2

When the line angle = math.radians(angle) executes, this is replacing the value of the same angle variable that's being checked on the previous line - thus, since angle will never be higher than 360, the loop will never finish.

My recommendation is to change the name of the variable when you convert it to radians, so that you're not overwriting its value - such as like this: angle_radians = math.radians(angle). Thus, you'd also have to change your sin and cos to use the new variable as well.

1

First, as pointed out by @Random, you need to use a different variable name for angle = math.radians(angle). For example, angle_radians

Second, when you use print, you need to convert x_coord and y_coord to string: str(x_coord) and str(y_coord)

Try this:

import math

r = 5

angle = 0

while angle <= 360:

    angle_radians = math.radians(angle)

    x_coord = math.sin(angle_radians)*r

    y_coord = math.cos(angle_radians)*r
    print ("Position [",str(x_coord),str(y_coord),"]")
    angle +=1

Not the answer you're looking for? Browse other questions tagged or ask your own question.