calthoff / tstp

This is an old repository for the exercises in "The Self-Taught Programmer." Please see /selftaught.
166 stars 193 forks source link

Chapter 7, Challenge 5 question #4

Closed jonathan-j-stone closed 7 years ago

jonathan-j-stone commented 7 years ago

I was able to produce the exact same result as your solution:

list1 = [8, 19, 148, 4]
list2 = [9, 1, 33, 83]
list3 = []

for i in list1:
    for j in list2:
        mult = i * j
        list3.append(i * j)

print(list3)

. . . only I did not include the line: mult = i * j anywhere in my code.

Why is that?

calthoff commented 7 years ago

@ILeftTheLaw I used an extra variable called mult=i j to make the code easier to read for beginners. In your example, you are not using mult = i j for anything. In the original, mult got passed into append: list3.append(mult). You can also just append the value and get rid of the variable entirely: list3.append(1*j).