I wrote a program that takes two lists and multiplies each index in the first list by the every index in the second list. Once the second list reaches the last index the index in the first list is incremented to the next index and repeats the process.
What I am trying to do now is have it be recursive so that the number of lists won't matter and the program can enumerate itself recursively. I also want to show what what multiplication is being outputted. For now the output shows all the answers in a list but I also want to show it in the output say for example:
1 * 4 = 4
As the 0 index in the first list is 1 and the 0 index in the second list is 4.
5 * 11 = 55
1 index of first list times 2 index of the second list
Below is the full working code as well as the output:
Code:
list1 = [1, 5, 16, 14, 21]
list2 = [4, 7, 11, 14, 17]
# store multiplied values in new list
results = []
# assign two temp variables to keep track of each list index
temp1 = 0
temp2 = 0
# loop through each index of the first list
while temp1 <= len(list1) - 1:
# print(var1[temp1]*var2[temp2])
# add to results list the current index of list 1 * the current index of temp 2
results.append(list1[temp1]*list2[temp2])
# increase the index of the temp variables to traverse both lists in order
# temp2 gets incremented until it reaches the end last index of list2
# once it does increase temp1 by 1 to move onto the next index in list1 and repeat the process
if temp2 == len(list2) - 1:
temp1 += 1
temp2 = 0
else:
temp2 += 1
print(results)
Output:
[4, 7, 11, 14, 17, 20, 35, 55, 70, 85, 64, 112, 176, 224, 272, 56, 98, 154, 196, 238, 84, 147, 231, 294, 357]