Home > OS >  When iterating using multiple 'def's in 'class', the function is undefined
When iterating using multiple 'def's in 'class', the function is undefined

Time:11-16

First, before making the code into a class, I made it into a def. Since it works normally in this code, I thought that it would work normally even if I grouped it into a class.

winner = ''
def start():
    pla2 = 0
    pla1 = int(input("What is pla1's choice? 1 , 2 , 3 :"))
    if pla1 == 3:
        return 'winner 2'
   
    elif pla1 == 2:
        return player2_bet(pla1,pla2)           
    elif pla1 == 1:
        return player2_bet(pla1,pla2)



def player2_bet(pla1,pla2):
    pla2 = int(input("What is pla2's choice? 1 , 2 , 3 :"))
    if pla2 == 3:
        return 'winner 1'      
    elif pla2 == 2:
        if pla1 == 2:
            return 'check'
        elif pla == 1:
            return player1_bet(pla1,pla2)
    elif pla2 == 1:
        return player1_bet(pla1,pla2)

def calpage(pla1,pla2):
    if pla1 == pla2 == 2:
        Flop(deck)
    else:
        return player1_bet(pla1,pla2)

def player1_bet(pla1,pla2):
    pla1 = int(input("What is pla1's choice? 1 , 2 , 3 :"))
    if pla1 == 3:
        return 'winner 1'
    elif pla1 == 2:
        if pla2 == 2:
            return 'check'
        elif pla2 == 1:
            return player2_bet(pla1,pla2)
    elif pla1 == 1:
        return player2_bet(pla1,pla2)


print(start())

The code to make now is a code that two people press 1, 2, and 3 times. I tried to make a code that ends when one person presses 3 or two presses 2. When I didn't include the class for the first time, I confirmed that the code worked normally, But when grouped as a class

class twoplayer:
    winner = ''
    def start():
        pla2 = 0
        pla1 = int(input("What is pla1's choice? 1 , 2 , 3 :"))
        if pla1 == 3:
            return 'winner 2'
       
        elif pla1 == 2:
            return player2_bet(pla1,pla2)           
        elif pla1 == 1:
            return player2_bet(pla1,pla2)



    def player2_bet(pla1,pla2):
        pla2 = int(input("What is pla2's choice? 1 , 2 , 3 :"))
        if pla2 == 3:
            return 'winner 1'      
        elif pla2 == 2:
            if pla1 == 2:
                return 'check'
            elif pla == 1:
                return calpage(pla1,pla2)
        elif pla2 == 1:
            return calpage(pla1,pla2)
        
    def calpage(pla1,pla2):
        if pla1 == pla2 == 2:
            Flop(deck)
        else:
            return player1_bet(pla1,pla2)

    def player1_bet(pla1,pla2):
        pla1 = int(input("What is pla1's choice? 1 , 2 , 3 :"))
        if pla1 == 3:
            return 'winner 1'
        elif pla1 == 2:
            if pla2 == 2:
                return 'check'
            elif pla2 == 1:
                return player2_bet(pla1,pla2)
        elif pla1 == 1:
            return player2_bet(pla1,pla2)

who = twoplayer()
print(who)

This code pops up.

<__main__.twoplayer object at 0x0000026C0671A9E0>

The reason I'm trying to group it into a class now is to check that this code works normally and put it in another code. But I can't find the reason why it's wrong, and I don't understand it because it's too difficult or I haven't learned how to explain it on stackflow. Where am I wrong?

CodePudding user response:

When creating classes you need to pass in the self-method and you also need to call the start function in order to actually run it.

class twoplayer:
    winner = ''
    def start(self):
        pla2 = 0
        pla1 = int(input("What is pla1's choice? 1 , 2 , 3 :"))
        if pla1 == 3:
            return 'winner 2'
       
        elif pla1 == 2:
            return self.player2_bet(pla1,pla2)           
        elif pla1 == 1:
            return self.player2_bet(pla1,pla2)

    def player2_bet(self,pla1,pla2):
        pla2 = int(input("What is pla2's choice? 1 , 2 , 3 :"))
        if pla2 == 3:
            return 'winner 1'      
        elif pla2 == 2:
            if pla1 == 2:
                return 'check'
            elif pla1 == 1:
                return self.calpage(pla1,pla2)
        elif pla2 == 1:
            return self.calpage(pla1,pla2)
        
    def calpage(self,pla1,pla2):
        if pla1 == pla2 == 2:
            #Flop(deck) # some later code?
            pass
        else:
            return self.player1_bet(pla1,pla2)

    def player1_bet(self,pla1,pla2):
        pla1 = int(input("What is pla1's choice? 1 , 2 , 3 :"))
        if pla1 == 3:
            return 'winner 1'
        elif pla1 == 2:
            if pla2 == 2:
                return 'check'
            elif pla2 == 1:
                return self.player2_bet(pla1,pla2)
        elif pla1 == 1:
            return self.player2_bet(pla1,pla2)

who = twoplayer().start()
print(who)

Result:

What is pla1's choice? 1 , 2 , 3 :1
What is pla2's choice? 1 , 2 , 3 :2
What is pla1's choice? 1 , 2 , 3 :3
winner 1
  • Related