-1

We're supposed to plot the sine wave with the help of Matplotlib but in degrees.

First we're supposed to get every sin(x) from 0-360 degrees, every ten degrees.

I'm a beginner, would really appreciate the help.

def getsin():
    for i in range(0,361,10):
        y=math.sin(math.radians(i))
        sinvalue.append(round(y,3)
sinvalue=[]
getsin()


x=np.linspace(0,360,37)
plot.plot(x,sinvalue)
plot.xlabel("x, degrees")
plot.ylabel("sin(x)")
plot.show()
gboffi
  • 22,939
  • 8
  • 54
  • 85
struppis
  • 11
  • 1
  • 1
    In your code example you don't need to define a function. Just remove that line and declare `sinvalue` before. The rest seems perfectly fine to me – Nick Skywalker Nov 12 '19 at 17:29
  • lol ok that solved the problem, thanks! could you explain to me why it made a diffrenece to not define a function? because the output in sinvalue should be the same with or without function.. – struppis Nov 12 '19 at 17:43
  • Why did you edit half of your question away? You chopped it off in the middle of a word and removed all code. – SyntaxVoid Nov 12 '19 at 18:14

1 Answers1

2

Your code is almost OK (you missed a closing parenthesis on line sinvalue.append(round(y,3)), but it can be perfected.

E.g., it's usually considered bad practice updating a global variable (I mean sinvalue) from inside a function... I don't say that it should never be done, I say that it should be avoided as far as possible.

A more common pattern is to have the function to return a value...

def getsin():
    sines = []
    for i in range(0,361,10):
        y=math.sin(math.radians(i))
        sines.append(round(y,3))
    return sines

Why I say this is better? Compare

sinvalue=[]
getsin()

with

sinvalue = getsin()

At least in my opinion, the second version is waaay better because it makes clear what is happening, now sinvalue is clearly the result of getting the sines...

gboffi
  • 22,939
  • 8
  • 54
  • 85