<...object at 0x...> appears when executing simple game code

Asked 2 years ago, Updated 2 years ago, 29 views

class game_system:
    def_init__(self, player, monster, p_hp, m_hp):
        self.player=player
        self.monster=monster
        self.p_hp = p_hp
        self.m_hp = m_hp
       
        while p_hp<0 or m_hp<0:
            p_attack=int(input("enter number:"))
            m_attack=int(input("enter number:"))
            self.p_hp-=m_attack
            self.m_hp-=p_attack
            turn = 1
            print(f "Attack {self.monster} on {turn} turn {self.player} was damaged by {p_attack}")
            print(f "Attack {self.player} on {turn} turn {self.monster} was damaged by {m_attack}")
            turn + = 1
a=game_system
print(a("t", "s", 50, 20))

python

2022-09-30 20:18

2 Answers

  • a contains an instance of game_system
  • while p_hp<0 or m_hp<0: corrects to while self.p_hp>0 or self.m_hp>0: (with unequal sign correction and self.)
class game_system:
    def_init__(self, player, monster, p_hp, m_hp):
        self.player=player
        self.monster=monster
        self.p_hp = p_hp
        self.m_hp = m_hp
       
        while self.p_hp>0 or self.m_hp>0:
            p_attack=int(input("enter number:"))
            m_attack=int(input("enter number:"))
            self.p_hp-=m_attack
            self.m_hp-=p_attack
            turn = 1
            print(f "Attack {self.monster} on {turn} turn {self.player} was damaged by {p_attack}")
            print(f "Attack {self.player} on {turn} turn {self.monster} was damaged by {m_attack}")
            turn + = 1
a=game_system("t", "s", 50, 20)


2022-09-30 20:18

If the given source is everything, and it is not an error, but a message similar to the following, it should work correctly.

<_main__.game_system object at 0x000001E5421D9D00>

The direct cause that appears to be non-operational (and is actually only partially operational) is the reverse direction of the inequality in the following lines, which causes the while loop to never operate.
If you simply reverse the inequality sign, it may not be correct as a decision to continue the loop.
add
For example, if you just reverse the inequality and leave or between them, they will not end unless they are equal to or less than zero.

while p_hp<0 or m_hp<0:

Also, the variables for determining the while loop (p_hp, m_hp) and the variables that are being processed in the loop (self.p_hp, self.m_hp) are different.
Wouldn't this not end permanently even if I put it in a loop?

self.p_hp-=m_attack
    self.m_hp-=p_attack

Also turn=1 before print(...) in the while loop, so the turn number displayed is always 1.

<__main__...> appears because def_init__(...) does not return a return value and the contents of two lines calling it

The following lines do not have a ( ) after them (not a call as a function or method) and do not specify any parameters, so an instance of the game_system class will only be created and stored in the variable a.

a=game_system

The following line specifies parameters that match def_init__(...), so def_init__(...) runs for the first time.

print(a("t", "s", 50, 20))

However, since the above inequalities are in the opposite direction, after initializing the internal variables, they end up doing nothing.

And the def__init__(...) in this class does not return anything when it is finished, so print(a(...) will display the object a information and the above <__main__...> will be displayed.

Furthermore, even if the while loop is operated by correcting the direction of the inequality above and the variables being processed, there is no return value, so you will see the <__main__...> above after the loop ends.

It seems that the intention of the questioner is quite obvious, but I tried the following as an example of the change.
Comments in #### are added and changed.

class game_system:
    def_init__(self, player, monster, p_hp, m_hp):
        self.player=player
        self.monster=monster
        self.p_hp = p_hp
        self.m_hp = m_hp
        
    deffight(self):#### Initialize and fight are independent methods
        The self.turn=0####turn variable is initialized as an instance variable before while is processed.
        While self.p_hp>0 and self.m_hp>0:####Reviewing the While Loop Continue Conditions
            p_attack=int(input("enter the player's attack points:")#### Clarify which attack point input is entered
            m_attack=int(input("enter the monster's attack points:")####
            self.p_hp-=m_attack
            self.m_hp-=p_attack
            self.turn+=1#### Go here to update the number of turns
            print(f "Attack {self.monster} on {self.turn} turn {self.player} was damaged by {p_attack}")
            print(f "Attack {self.player} on turn {self.monster} was damaged by {m_attack}")
        
        #### A processing for determining the result and a return value notification are added below.Winning/Losing is a player's point of view
        self.result='"
        if self.turn == 0:
            self.result = 'no battery occur'
        elif self.p_hp<=0 and self.m_hp<=0:
            self.result = 'double knockout'
        elifself.p_hp>self.m_hp:
            self.result='won'
        elifself.p_hp<self.m_hp:
            self.result='lost'
        
        #### Number of battle turns, player's perspective result, player's remaining HP is notified
        return self.turn,self.result,self.p_hp

a=game_system("t", "s", 50, 20)#### Specify initialization parameters when creating objects
print(a.fight())### Call the battle method to view the results
#### If you don't separate the variables that store the results, they will appear as tuples.
#### You can change the above print(a.fight()) as follows.
#### t,r,p = a.fight()
#### print(f'number of battle turns: {t}, result: {r}, player remaining HP: {p}')


2022-09-30 20:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.