I am trying to solve the following exercise:
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
Example 1:
Input: nums = [1,2,3,4]
Output: [24,12,8,6]
Example 2:
Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]
Here is my code:
def inputArray():
# creating an empty list
lst = []
# number of elements as input
n = int(input("Enter number of elements : "))
# iterating till the range
for i in range(0, n):
ele = int(input())
lst.append(ele) # adding the element
print(lst)
return lst
l=inputArray()
array=[]
m=1
mul=1
for i in l:
m*=i
mul=m/i
for j in range(0,len(l)-1):
array[j]=mul
mul=1
print(array)
And I have the following eror: list assignment index out of range. I hope anyone could help me...
array[j]to create a new element. Usearray.append(mul)