Ask coding questions

← Back to all posts
Why is it showing name-error, i have already defined 'user_cards' on line 79.
Iamvik98

Traceback (most recent call last):
File "main.py", line 110, in <module>
play()
File "main.py", line 105, in play
compare(user_score,computer_score)
File "main.py", line 73, in compare
return f"You Lose.Your hand is {user_cards}.Opponent hand is {computer_cards}."
NameError: name 'user_cards' is not defined

"user_cards" is defined on line 79.

Answered by ch1ck3n [earned 5 cycles]
View Answer
Voters
Iamvik98
Comments
hotnewtop
ch1ck3n

uncomment line 76 so it will be

Iamvik98

Still gettinn' it [email protected]

ch1ck3n

@Iamvik98 here is the fixed code

https://replit.com/@ch1ck3n/blackjack-start-2#main.py

look at what I did in line 40 and 41

now it is a global variable, and every function can access it
read more here https://www.w3schools.com/python/python_variables_global.asp

Iamvik98

Literally the 1st thing defined in play() is [email protected]

ch1ck3n

@Iamvik98

but since you defined it first in the function, the other functions cannot access it. it's called scopes

Iamvik98

After your amendments on line 40 and 41, it's definitely not showing no errors. But in the final printed result in the console you can see blank lists being printed as final cards of user and [email protected]

ch1ck3n

@Iamvik98 oh kay??
i am sorry i do not know how to play blackjack and i don't know what you'r doing to get to the position you are in

Iamvik98

I have turned both user_cards and computer_cards as global variable using "global" keyword, now the code is working properly. Thanks for sharing the w3schools link. You're a stud bro! @ch1ck3n

Iamvik98

It's working now, thanks for your help. It helped me find the solution. @ch1ck3n

ch1ck3n

@Iamvik98 no prob

Iamvik98

@ch1ck3n gud man u !