Questions about function definition order and invocation

Asked 2 years ago, Updated 2 years ago, 17 views

"The code below is related to ""war"" in the ""self-taught programmer"", but I have a question."Below are the two intra-class functions.I'm still a beginner in programming, and it may be quite rudimentary, but I appreciate your cooperation.

Although there are play_game and winner functions inside the Game class, I have an image that if I want to get something, I can only get it above the appropriate one by Python specification.

However, if you look at the previous two functions, you will find the winner function below the play_game function.Is this a phenomenon caused by trying to get a value in the class?Or is there something fundamentally wrong?Please let me know.

 class Game:

    defplay_game(self):
        cards = self.decks.cards
        print('Start War'')
        whilelen(cards)>=2:
            US>Exit with m='q, play with other keys:'
            response=input(m)
            if response=='q':
                break
            self.p1.card = self.deck.draw()
            self.p2.card = self.deck.draw()
            self.print_draw (self.p1, self.p2)
            if self.p1.card>self.p2.card:
                self.p1.wins+=1
                self.print_winner (self.p1)
            else:
                self.p2.wins+=1
                self.print_winner (self.p2)
    
        win=self.winner (self.p1, self.p2)
        print('End of Game, Victory of {}'.format(win)))
    
    def winner (self, p1, p2):
        if p1.wins>p2.wins:
            return p1.name
        if p1.wins<p2.wins:
            return p2.name
        "Return" draw!'

python

2022-09-30 16:10

1 Answers

Is this a phenomenon caused by trying to get a value in the class?

No, it's not.The same goes for simple functions.

Or is there something fundamentally wrong?

It doesn't have to be physically written above, and it doesn't matter if it's loaded before the function call.

def funcA():
    print('funcA')
    funcB()

def funcB():
    print('funcB')

funcA()

There is no problem with , but

def funcA():
    print('funcA')
    funcB()

funcA()

funcA()

def funcB():
    print('funcB')

cannot call funcB() within funcA().


2022-09-30 16:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.