2

I was trying to do a basic spinning animation using "\|/-" which went fine, but I wanted two on the same updating line. With one spinning clockwise and one spinning anti-clockwise.

Here is what I came up with:

animation_1 = ['\\', '|', '/', '-']
animation_2 = ['/', '|', '\\', '-']

while True:
    for iteration_1, iteration_2 in animation_1, animation_2:
        print(f'\r    {iteration_2}  {iteration_1}', end='')
        sleep(.5)
        sys.stdout.flush()

Which get the output:

"Traceback (most recent call last):
  File "/Users/pungkrock/PycharmProjects/ans/fib.py", line 18, in <module>
    for iteration_1, iteration_2 in animation_1, animation_2:
ValueError: too many values to unpack (expected 2)"

This example:

animation_1 = ['\\', '|', '/', '-']
animation_2 = ['/', '|', '\\', '-']

while True:
    for iteration_1, iteration_2 in animation_1:
        print(f'\r    {iteration_2}  {iteration_1}', end='')
        sleep(.5)
        sys.stdout.flush()

Gets the output:

"Traceback (most recent call last):
  File "/Users/pungkrock/PycharmProjects/ans/fib.py", line 18, in <module>
    for iteration_1, iteration_2 in animation_1:
ValueError: not enough values to unpack (expected 2, got 1)"

And this error massage I do understand. But I don't understand why I get an error message from the first example.

This example works fine:

animation_1 = ['\\', '|', '/', '-']
# animation_2 = ['/', '|', '\\', '-']

while True:
    for iteration_1 in animation_1:
        print(f'\r      {iteration_1}', end='')
        sleep(.5)
        sys.stdout.flush()

Can some kind soul explain to me way the first code example doesn't work? Or if I'm missing some basic understanding of some concept of the language? "too many values to unpack (expected 2)", it did get 2? What am I missing?

kaya3
  • 47,440
  • 4
  • 68
  • 97
Pungkrock
  • 23
  • 3
  • 2
    You're just missing the function `zip`, as in ` for iteration_1, iteration_2 in zip(animation_1, animation_2) : ...` – Demi-Lune Feb 19 '20 at 16:10
  • So in the end, this question turns out to be a duplicate of https://stackoverflow.com/questions/1663807/how-to-iterate-through-two-lists-in-parallel – Demi-Lune Feb 19 '20 at 18:27
  • Does this answer your question? [How to iterate through two lists in parallel?](https://stackoverflow.com/questions/1663807/how-to-iterate-through-two-lists-in-parallel) – juanpa.arrivillaga Feb 20 '20 at 00:19

3 Answers3

1

I don't understand why I get an error message from the first example.

OK, let's walk through it. The relevant parts are that you have two lists of length 4, and then you tried to iterate over both of them with something like this:

a = [1, 2, 3, 4]
b = [5, 6, 7, 8]

for i, j in a, b:
    ...

I'll use this code as an example because it's a bit simpler. There are two key points:

  • a, b creates a tuple containing the two lists. It's the same as if you wrote (a, b).
  • for i, j in something only works if each element of something is a sequence of length 2.

From there, it's quite straightforward: the elements of the tuple (a, b) are simply a and b themselves, which are sequences of length 4, not sequences of length 2. So, that's why there are too many values to unpack: 4 is too many.

kaya3
  • 47,440
  • 4
  • 68
  • 97
0
>>> for x in range(4):
...   print(animation_1[x],animation_1[3-x])
...
\ -
| /
/ |
- \

sorry, no explanation, i am new to python meself too....

Luuk
  • 12,245
  • 5
  • 22
  • 33
0

in animation_1, animation_2 is not valid syntax

I assume you want to zip them

for a1, a2 in zip(animation_1, animation_2)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    It is valid syntax; it creates a tuple `(animation_1, animation_2)` and iterates over that tuple. The "too many values to unpack" error is because each element of that tuple isn't a sequence of length 2. – kaya3 Feb 19 '20 at 16:15
  • Thank you sir! You need to use the zip function, I'll keep that in mind. I really appreciate it! – Pungkrock Feb 19 '20 at 16:40
  • @kaya3 Feel free to provide your own answer – OneCricketeer Feb 19 '20 at 21:32