Getting unresolved reference
Question:
For some reason I am getting an unresolved reference ‘i’ error here in python 3.9.x
How can I amend my code to get it to run without error?
for card in range(len(cards)):
card = cards[i]
print(f"{i+1} - {card}")
# get choice
choice = get_int_input(1, len(cards))
# get card
return cards[choice-1] if choice else None
”’
Answers:
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’ |
You don’t need to index your card
value.
You need to adjust the first line of your loop as such:
for i in range(len(cards)):
card = cards[i]
print(f"{i+1} - {card}")
but this is not the most pythonic way. Instead what you can do is just call cards directly, and use enumerate if you need the index.
for index, card in enumerate(cards):
print(f"{index+1} - {card}")
try changing the for loop to:
for i in range(len(cards)):
For some reason I am getting an unresolved reference ‘i’ error here in python 3.9.x
How can I amend my code to get it to run without error?
for card in range(len(cards)):
card = cards[i]
print(f"{i+1} - {card}")
# get choice
choice = get_int_input(1, len(cards))
# get card
return cards[choice-1] if choice else None
”’
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’ |
You don’t need to index your card
value.
You need to adjust the first line of your loop as such:
for i in range(len(cards)):
card = cards[i]
print(f"{i+1} - {card}")
but this is not the most pythonic way. Instead what you can do is just call cards directly, and use enumerate if you need the index.
for index, card in enumerate(cards):
print(f"{index+1} - {card}")
try changing the for loop to:
for i in range(len(cards)):