Python is right. You has not i
variable. Where do you see it?
If cards contains 4 element, your card
will take successively 0
, 1
, 2
, 3
. So change your for loop like that:
for i in range(len(cards)):
card = cards[i]
print(f"{i+1} - {card}")
But the best way to do what you want is like that:
for i, card in enumerate(cards):
print(f"{i+1} - {card}")
enumerate
function will create an iterable variable that contains tuples of (key, value)
. For example:
>>> print(list(enumerate(['a', 'b', 'c'])))
[(0, 'a'), (1, 'b'), (2, 'c')]
In the same example, this loop:
for i, letter in enumerate(['a', 'b', 'c'])
will take successively this variable:
| iterations | i | letter |
| ---- | ------ | ----- |
| 1 | 0 | 'a' |
| 2 | 1 | 'b' |
| 3 | 2 | 'c' |