fiestabowl
fiestabowl

Reputation: 1

error Multiplying elements of one list by another

Given two lists of integers, write a function multLists(list1, list2) that multiplies each value of the list1 by the values of list2 and returns list1 with it's updated values.

def multLists(list1, list2):
    for i in range(len(list1)):
        list1 = list1[i] * list2[i]
    return list1

length = int(input())
first_list = []
second_list = []
for i in range(length):
    num1 = int(input())
    num2 = int(input())
    first_list.append(num1)
    second_list.append(num2)
list1 = multLists(first_list, second_list)
for i in list1:
    print(i)

When submitting, this is what occurs:

2
1
2
3
4
Your output
Traceback (most recent call last):


     File "main.py", line 17, in <module>
    list1 = multLists(first_list, second_list)
  File "main.py", line 3, in multLists
    list1 = list1[i] * list2[i]
TypeError: 'int' object is not subscriptable

Your output does not contain
[8, 24]

Upvotes: 0

Views: 279

Answers (1)

Cowabunghole
Cowabunghole

Reputation: 193

In the line list1 = list1[i] * list2[i], you are setting list1 (which is a list) to the result of list1[i] * list2[i], which is a number. Maybe you meant list1[i] = list1[i] * list2[i]?

Upvotes: 1

Related Questions