I am playing a Python Blackjack game but when I run the program I get an import error for the two variables that I need for another function, I have already tried making them global but the error still comes up. What I can do?
In the players.py module I have the following:
#Módulo Jugadores
def Jugadores():
""" Esta función permite ingresar el número de jugadores y el nombre de cada uno. """
Número_Jugadores=int(input(f"Ingrese el número de jugadores:"))
Nombre_Jugadores=[]
print("")
for i in range(Número_Jugadores):
global Nombre_Jugador
Nombre_Jugador=input(f"Ingrese el nombre del jugador {i+1}:")
Nombre_Jugadores.append(Nombre_Jugador)
In the module letters.py I have the following:
#Módulo Cartas
def Cartas():
""" Esta función permite generar el mazo de cartas. """
Valor_Cartas=[2,3,4,5,6,7,8,9,10,"J","Q","K","A"]
Palos=["Picas","Corzanones","Diamantes","Tréboles"]
global Mazo_de_Cartas
Mazo_de_Cartas=[]
for Valor in Valor_Cartas:
for Palo in Palos:
Carta="{} de {}".format(Valor,Palo)
Mazo_de_Cartas.append(Carta)
In the shifts.py module I have the following:
#Módulo Turnos
import random
import time
from jugadores import Nombre_Jugador
from cartas import Mazo_de_Cartas
def Turno():
Total=0
Elección=1
i=1
Mazo_de_Cartas
Cartas_Jugador=[]
print(f"Turno de {Nombre_Jugador}:")
while Elección!=0 and Total<21:
Valor_Carta=0
print (f"\nCarta #{i}:")
time.sleep(3)
Carta=random.choice(Mazo_de_Cartas)
print(f"[Carta]")
When I run the program I get the following:
Traceback (most recent call last):
File "main.py", line 7, in <module>
from turnos import Turno
File "/home/runner/Taller-03/turnos.py", line 6, in <module>
from jugadores import Nombre_Jugador
ImportError: cannot import name 'Nombre_Jugador' from 'jugadores' (/home/runner/Taller-03/jugadores.py)
You declare the global variable
Nombre_Jugador
inside the functionJugadores
in:this implies that
Nombre_Jugador
it does not exist until the function is called . When you import therefore it does not exist, the body of a function is not evaluated when a module is imported, it is only evaluated when it is executed.However, using global variables is a very bad practice if it is not for state variables or constants, it hinders the readability of the code and makes it prone to bugs that are difficult to debug in many cases.
Also, keep in mind that global variables in Python are only global variables at the module level, not at the process level. Therefore, modifying the variable in the main module does not affect the value it has in the rest.
You don't need to make those variables global, make functions return things and accept arguments, more readable, more scalable and safer. Something like:
jugadores.py
cartas.py
turnos.py
main.py
I also recommend you follow the style conventions defined in:
The names of modules, functions, methods and variables must be in lowercase and used
_
as a separator. HeCamelCase
is reserved for naming classes andNOMBRE_CONSTANTE
for constants.If possible, always use four spaces per indentation level.
For more information on module/package import errors see: