1

I'm a beginner of python and follow a book to practice. In my book, the author uses this code

s, k = 0

but I get the error:

Traceback (most recent call last):  File "<stdin>", line 1, in
<module> TypeError: 'int' object is not iterable

I want to know what happened here.

martineau
  • 112,593
  • 23
  • 157
  • 280
Hongmin Li
  • 13
  • 3

3 Answers3

1

You are asking to initialize two variables s and k using a single int object 0, which of course is not iterable.

The corrrect syntax being:

s, k = 0, 0

Where

s, k = 0, 1

Would assign s = 0 and k = 1

Notice the each int object on the right being initialized to the corresponding var on the left.

OR

s,k = [0 for _ in range(2)]

print(s)    # 0
print(k)    # 0
DirtyBit
  • 16,151
  • 4
  • 28
  • 54
0
s = k = 0

OR

s, k = (0, 0)

depends on what u need

vi_me
  • 363
  • 6
  • 16
0

Insted of:

s, k = 0

Use:

s, k = 0,0
Anonymous
  • 561
  • 5
  • 16