python - Python / TypeError:无法解压不可迭代的 Jugador 对象
问题描述
嘿伙计们,我试图在 for 循环中创建 N 个对象,但它给了我一个错误TypeError: cannot unpack non-iterable Jugador object
。
你能帮我吗?
我做错了什么?
这是我的代码:
from Jugador import Jugador
class Juego():
tipoJuego = ""
nJugadores = ""
def __init__(self,tipoJuego, nJugadores):
self.tipoJuego = tipoJuego
self.nJugadores = nJugadores
nJugadoresInt = int(nJugadores)
tipoJuegoInt = int(tipoJuego)
if tipoJuegoInt == 1 or tipoJuegoInt == 2 or tipoJuegoInt == 3:
print("Has elegido el tipo de juego ",tipoJuegoInt, ", y van a jugar ", nJugadores, " Personas")
else:
print("Error: Tipo de juego invalido")
exit()
for i in range(1,nJugadoresInt):
print("Jugador",i, "introduzca el nombre de usuario:")
nUsu = input()
print("Introduzca la edad:")
age = input()
print("Introduzca la palabra elegida:")
word = input()
J,i = Jugador(nUsu, age, word)
解决方案
如果您打算创建一种方法来分配 N 个对象以便稍后找到它们,我可以建议将它们存储在一个列表中J
:
from Jugador import Jugador
class Juego():
tipoJuego = ""
nJugadores = ""
def __init__(self,tipoJuego, nJugadores):
self.tipoJuego = tipoJuego
self.nJugadores = nJugadores
nJugadoresInt = int(nJugadores)
tipoJuegoInt = int(tipoJuego)
if tipoJuegoInt == 1 or tipoJuegoInt == 2 or tipoJuegoInt == 3:
print("Has elegido el tipo de juego ",tipoJuegoInt, ", y van a jugar ", nJugadores, " Personas")
else:
print("Error: Tipo de juego invalido")
exit()
J = []
for i in range(1,nJugadoresInt):
print("Jugador",i, "introduzca el nombre de usuario:")
nUsu = input()
print("Introduzca la edad:")
age = input()
print("Introduzca la palabra elegida:")
word = input()
J.insert(i,Jugador(nUsu, age, word))
供参考:https ://docs.python.org/3.8/tutorial/datastructures.html
推荐阅读
- ssis - 根据文件日期选择加载excel文件
- react-native - 不变违规元素类型无效:需要字符串(对于内置组件)或类/函数(对于复合组件)
- android - Android Studio Proguard 配置文件
- regex - 使用正则表达式获取所有 HTML 标记之间的所有数字
- excel - Issue with VB macros in Excel not selecting the correct cells
- c# - 使用 newsoft.json 进行序列化和反序列化
- powershell - PowerShell Get-ChildItem with variable and string
- windows - 将新行添加到文本文件时运行 Windows 命令?
- r - 从一列名称中创建一列首字母作为R中的字符串
- c - 如何将两个位图与 80-20% 的 AVX2 混合?