Python - Problema con un método de instancia

 
Vista:

Problema con un método de instancia

Publicado por David (4 intervenciones) el 27/05/2021 14:28:45
Hola, tengo la siguiente duda:
-Creo 3 clases diferentes
- En una de las clases, utilizo otra clase como atributo
-En dicha clase. al usar un método(atttack()),debería devolver un valor entero, pero el caso es que me da el siguiente error: TypeError: '>' not supported between instances of 'NoneType' and 'int'
- He probado a hacer un casting int al valor devuelto por dicho método, sin embargo sigue sin funcionar.
Pongo el código por si alguien puede echarme una mano:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import random
 
class Weapon():
    def __init__(self,name,damage,scope):
        self.name=name
        self.damage=int(damage)
        if scope=="short" or scope=="long":
            self.scope=scope
 
class Pirate():
    boat_life=200
 
    def __init__(self,name,W):
        self.name=name
        self.weapon=W
 
    def attack(self,enemigo):
        if (self.weapon.scope=="short" and enemigo.position=="near") or (self.weapon.scope=="long" and enemigo.position=="far") or (self.weapon.scope=="long" and enemigo.position=="near"):
            print("{} ha atacado a {}".format(self.name,enemigo.name))
            return int(self.weapon.damage)
 
 
 
    def hit(self,daño):
       self.boat_life=self.boat_life-daño
 
class Enemy():
    def __init__(self,name,health,position,min_damage,max_damage):
        self.name=name
        self.health=health
        if position=="near"or position=="far":
            self.position=position
        self.min_damage=int(min_damage)
        self.max_damage=int(max_damage)
 
    def attack(self,Pirate):
        daño=random.randint(0,1)
        print("¡Oh no! {} ha atacado a {}".format(self.name,Pirate.name))
 
        if daño==0:
            return self.min_damage
        else:
            return self.max_damage
    def move(self):
        movimiento=random.randint(0,1)
        if movimiento==0:
            self.position="near"
        else:
            self.position="far"
 
 
    def hit(self,daño):
        self.health=self.health-daño
 
 
 
 
Espada=Weapon("Espada",3,"short")
Hacha=Weapon("Hacha",5,"short")
Arco=Weapon("Arco",2,"long")
 
Torvellino1=Enemy("Torvellino1",25,"near",2,5)
Torvellino2=Enemy("Torvellino2",25,"near",4,8)
Torvellino3=Enemy("Torvellino3",25,"far",3,5)
Torvellino4=Enemy("Torvellino4",25,"far",6,9)
 
Pyratilla=Pirate("pyratilla",Espada)
Pym=Pirate("pym",Arco)
Pyerce=Pirate("pyerce",Hacha)
 
ListaPiratas=[Pyratilla,Pym,Pyerce]
ListaEnemigos=[Torvellino1,Torvellino2,Torvellino3,Torvellino4]
indice=0
 
def restart_battle():
    """Se encarga de regenerar la batalla
    """
    Pyratilla.boat_life=200
    Pym.boat_life=200
    Pyerce.boat_life=200
    Torvellino1.health=25
    Torvellino2.health=25
    Torvellino3.health=25
    Torvellino4.health=25
    ListaPiratas=[Pyratilla,Pym,Pyerce]
    ListaEnemigos=[Torvellino1,Torvellino2,Torvellino3,Torvellino4]
 
 
 
while len(ListaPiratas)>0 and len(ListaEnemigos)>0:
    if len(ListaPiratas)<3:
        restart_battle()
 
    else:
        indice=0
        for pirata in ListaPiratas:
            if ListaEnemigos[indice].health>0:
                valor=pirata.attack(ListaEnemigos[indice])
                if valor>0:
                    ListaEnemigos[indice].hit(valor)
 
            else:
                ListaEnemigos.pop(indice)
        indicePiratas=random.randint(0,2)
        for enemigos in ListaEnemigos:
            decision=random.randint(0,1)
            if decision==0:
                enemigos.move()
            else:
                    valor=enemigos.attack(ListaPiratas[indicePiratas])
                    ListaPiratas[indicePiratas].hit(valor)
                    if ListaPiratas[indicePiratas].boat_life<=0:
                        ListaPiratas.pop(indicePiratas)
 
 
if len(ListaEnemigos)==0:
    print("¡¡¡La victoria es de los Pyrates")
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de bl4ckdrvg0n
Val: 425
Bronce
Ha mantenido su posición en Python (en relación al último mes)
Gráfica de Python

Problema con un método de instancia

Publicado por bl4ckdrvg0n (109 intervenciones) el 27/05/2021 14:57:47
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import random
 
class Weapon():
    def __init__(self, name, damage, scope):
        self.name = name
        self.damage = int(damage)
        if scope == "short" or scope == "long":
            self.scope = scope
 
 
class Pirate():
    boat_life = 200
 
    def __init__(self, name, W):
        self.name = name
        self.weapon = W
 
    def attack(self, enemigo):
        if (self.weapon.scope == "short" and enemigo.position == "near") or (self.weapon.scope == "long" and enemigo.position == "far") or (self.weapon.scope == "long" and enemigo.position == "near"):
            print("{} ha atacado a {}".format(self.name, enemigo.name))
        return int(self.weapon.damage)
 
    def hit(self, daño):
        self.boat_life = self.boat_life-daño
 
 
class Enemy():
    def __init__(self, name, health, position, min_damage, max_damage):
        self.name = name
        self.health = health
        if position == "near" or position == "far":
            self.position = position
            self.min_damage = int(min_damage)
            self.max_damage = int(max_damage)
 
    def attack(self, Pirate):
        daño = random.randint(0, 1)
        print("¡Oh no! {} ha atacado a {}".format(self.name, Pirate.name))
 
        if daño == 0:
            return self.min_damage
        else:
            return self.max_damage
 
    def move(self):
        movimiento = random.randint(0, 1)
        if movimiento == 0:
            self.position = "near"
        else:
            self.position = "far"
 
    def hit(self, daño):
        self.health = self.health-daño
 
 
Espada = Weapon("Espada", 3, "short")
Hacha = Weapon("Hacha", 5, "short")
Arco = Weapon("Arco", 2, "long")
 
Torvellino1 = Enemy("Torvellino1", 25, "near", 2, 5)
Torvellino2 = Enemy("Torvellino2", 25, "near", 4, 8)
Torvellino3 = Enemy("Torvellino3", 25, "far", 3, 5)
Torvellino4 = Enemy("Torvellino4", 25, "far", 6, 9)
 
Pyratilla = Pirate("pyratilla", Espada)
Pym = Pirate("pym", Arco)
Pyerce = Pirate("pyerce", Hacha)
 
ListaPiratas = [Pyratilla, Pym, Pyerce]
ListaEnemigos = [Torvellino1, Torvellino2, Torvellino3, Torvellino4]
indice = 0
 
 
def restart_battle():
    """
    Se encarga de regenerar la batalla
    """
    global Pyratilla
    global Pym
    global Pyerce
    global Pyerce
    global Torvellino1
    global Torvellino2
    global Torvellino3
    global Torvellino4
    global ListaPiratas
    global ListaEnemigos
 
    Pyratilla.boat_life = 200
    Pym.boat_life = 200
    Pyerce.boat_life = 200
    Torvellino1.health = 25
    Torvellino2.health = 25
    Torvellino3.health = 25
    Torvellino4.health = 25
    ListaPiratas = [Pyratilla, Pym, Pyerce]
    ListaEnemigos = [Torvellino1, Torvellino2, Torvellino3, Torvellino4]
 
 
while len(ListaPiratas) > 0 and len(ListaEnemigos) > 0:
    if len(ListaPiratas) < 3:
        restart_battle()
    else:
        indice = 0
        for pirata in ListaPiratas:
            if ListaEnemigos[indice].health > 0:
                valor = pirata.attack(ListaEnemigos[indice])
                if valor > 0:
                    ListaEnemigos[indice].hit(valor)
            else:
                ListaEnemigos.pop(indice)
                indicePiratas = random.randint(0, 2)
                for enemigos in ListaEnemigos:
                    decision = random.randint(0, 1)
                    if decision == 0:
                        enemigos.move()
                    else:
                        valor = enemigos.attack(ListaPiratas[indicePiratas])
                        ListaPiratas[indicePiratas].hit(valor)
                        if ListaPiratas[indicePiratas].boat_life <= 0:
                            ListaPiratas.pop(indicePiratas)
    if len(ListaEnemigos) == 0:
        print("¡¡¡La victoria es de los Pyrates")
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Problema con un método de instancia

Publicado por David (4 intervenciones) el 27/05/2021 15:10:15
Gracias, ahora se ve mucho más claro.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de bl4ckdrvg0n
Val: 425
Bronce
Ha mantenido su posición en Python (en relación al último mes)
Gráfica de Python

Problema con un método de instancia

Publicado por bl4ckdrvg0n (109 intervenciones) el 27/05/2021 15:13:26
Probalo ahora y nos comentas. En la función restart_battle faltaba agregar:
1
2
3
4
5
6
7
8
9
10
global Pyratilla
    global Pym
    global Pyerce
    global Pyerce
    global Torvellino1
    global Torvellino2
    global Torvellino3
    global Torvellino4
    global ListaPiratas
    global ListaEnemigos

Para hacer referencia que las variables que se van a modificar son las del scope global.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Problema con un método de instancia

Publicado por David (4 intervenciones) el 27/05/2021 15:17:50
El fallo me lo da en la línea 107 con el siguiente mensaje:
TypeError: '>' not supported between instances of 'NoneType' and 'int'
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Problema con un método de instancia

Publicado por David (4 intervenciones) el 27/05/2021 15:38:59
Pues el caso es que aparte de lo de las variables globales, no veo ninguna diferencia con respecto al código original.
He copiado el código que has puesto y también me funciona, muchas gracias, pero sigo sin entender por que me daba ese fallo.
Pero muchas gracias por tu respuesta y tu trabajo!!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar