Python/TypeError:无法解压缩不可编辑的Jugador对象

2024-05-29 11:39:09 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,伙计们,我试图在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)

Tags: orselfforinputdeelintprint
1条回答
网友
1楼 · 发布于 2024-05-29 11:39:09

如果您打算创建一种方法来分配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

相关问题 更多 >

    热门问题