0

Note: not all of the implementation is included. I have a variable, input, which is taken from user input. I copy input to a new list, new, and I make changes to new. But, when I print out and index of input (in the last line of calculateSine), it's changed.

input = getInput()
def alterInput(new):
    if PI/2 <= new[0] <= 3*PI/2:
        new[0] = PI- new[0]  
        return new
    if 3*PI/2 <= new[0] <= 2*PI: 
        new[0] = new[0] - 2*PI
        return new
    else:
        return new

def calculateSine():
    new = input[:]
    a = alterInput(new)
    count = 1 
    sum = 0
    for x in range(a[1]+1):
        if count%2 == 1:
            if count%4 == 3: 
                sum -= (a[0]**count)/(getFactorial(count))
            else:
                sum += (a[0]**count)/(getFactorial(count))
        count += 1   
    return "sin(" + str(input[0]) + ") = " + str(sum)

##OUTPUTS
print("")
print(calculateSine())
Geeky Quentin
  • 726
  • 1
  • 10
wadle
  • 9
  • 2

1 Answers1

0

First of never use built-in function names for variables (input in your case). Secondly to answer your question if you are trying to work with lists just assigning it to the new variable does not mean there are 2 lists stored in the memory they are both referencing the same list, so any modification to the list changes the original list they are referencing. Try this:

a = [1,2,3]
b = a.copy() # Now you can modify 'b' without worrying changing the original list
# If you have nested lists inside the list you can use deepcopy
from copy import deepcopy
a = [[1], [2], [3]]
b = deepcopy(a)