title: Compute cosine without import math in python
body: <p>I have a general idea of what to do, but my code is a mess and I'm having some trouble writing the algorithm in python for </p>
  
  <pre><code>cos(x)=1-(x^2)/2!+(x^4)/4!-(x^6)/6!+... 
  </code></pre>
  
  <p>where <code>x</code> is in radians, computing <code>cos(x)</code> after 20 terms using while loops. So far what I've written is</p>
  
  <pre><code>x = float(input("Enter a value for x in degrees."))
  x = (x*3.14159)/180
  num_of_terms = 0
  num = 1.0 ##numerator
  y = 1.0
  cosx = 1.0
  
  while num_of_terms&lt;1:
      num_of_terms+=1
      cosx = (num/y)
      while num_of_terms&gt;=1 and num_of_terms&lt;=20:
          num_of_terms+=1
          num = num*(x*x)
          y = y*num_of_terms*(num_of_terms-1)
          if num_of_terms%2==0:
              cosx = cosx+(-num/y)
          else:
              cosx = cosx+(num/y)
      print(cosx)
  </code></pre>
  
  <p>I don't know how close I even am to being correct (I know it's wrong in at least some places so I can't properly check using <code>math.cos</code>) but the main question I have is how to switch from positive --> negative each term. The assignment states that I cannot use exponentiation operators, and before I was trying to do something like</p>
  
  <pre><code>x = float(input("Enter a value for x in degrees."))
  x = (x*3.14)/180
  num_of_terms = 0
  y = 0
  z = 1
  cosx = ((-1)**(z-1))*((x**z)/(y))
  </code></pre>
  
  <p>so that the sign would switch for every other term. Now I have (as you can see above)</p>
  
  <pre><code>if num_of_terms%2==0:
      cosx = cosx+(-num/y)
  else:
      cosx = cosx+(num/y)
  </code></pre>
  
  <p>which is incorrect, or at least the output I'm getting is incorrect.</p>