Exercise 36 multiplying parts of lists

Hello!

I tried to make a quiz for this exercise. But I cannot multiply a part of a list with another part of the same list. Does anyone know how to solve this? Or work around it?

if choice == “1”:
print(“Very well. Let’s test your computational skills.\n Name a number from 1 to 10”)
memory = [int(input())]
print(“And another…”)
memory.append(int(input()))
print(“And another…”)
memory.append(int(input()))
print(“And another…”)
memory.append(int(input()))
print(“And another…”)
memory.append(int(input()))
print(“And another…”)
memory.append(int(input()))

for number in range (6):
    memory[number] = memory[number] + 3

print("""Ok, great.
Now add 3 to every number you just named.
Add the first three and the last three together.
Now multiply these two summations.
How much did your machine brain make of this?""")

answer1 = int(input())

if answer1 == memory[0:2] * memory[3:5]:

Thanks in advance!

You need to do the summation:

sum(memory[:3]) * sum(memory[3:])

Careful with the indices. The end index is excluded, “one off the end”, so to speak.

>>> l = [0, 1, 2]
>>> l[0:1]
[0]   # not [0, 1]
>>> l[:1]
[0]   # the same
>>> [1:]
[1, 2]
2 Likes

If you are using loops at this point, could you reduce that duplication in the user input? Just a thought.

1 Like

I most certainly would like to. How would you go about this?

something like this?:

for input in range(6):
memory = [int(input())]
print(“And another…”)

Yes, close. Don’t use input as the index variable for the loop (it’s a reserved name); and initialize the list beforehand.

memory = []
for _ in range(6):
    i = int(input("Give me an integer: ")
    memory.append(i)

Another way, probably a little clearer:

memory = []
while len(memory) < 6:
    # same as above

Or, if you’re on a quest for maximum terseness and/or minimum readability:

memory = [ int(input("Give me an integer: ") for _ in range(6) ]
1 Like

I was going to say, have a little play around and see what you can get working based on what you know so far.

But @florian went to town :grin:

1 Like

:smiley: Hehe. I guess I’m not much of a teacher!

1 Like