2

I made two classes that would call each other's function before releasing a result but I'm having trouble making it work, is there a reason why I could call the function of the first class above but not the function of the one below?
Here is the program:

import random

HAND = []
DECK = ['Ojama Red', 'Ojama Blue','Ojama Yellow','Ojama Green','Ojama Black', 'Ojama Pink', 'Ojama Lime']

class A:

    def func1(self):
        random.shuffle(DECK)
        B.func3()
    def func2(self):
        c = DECK[0]
        if c in DECK:
            HAND.append(c)
            DECK.remove(c) 

class B:
    def func3(self): # STANDBY
        self.func4()
    def func4(self): # DRAW
        A.func2()
        self.func5()
    def func5(self): # MAIN PHASE 1
        print('\n', HAND)
        print('\n', DECK)

start_game = A()
start_game.func1()
Reza Rahemtola
  • 1,182
  • 7
  • 16
  • 30
  • In function 4 you are calling function 2first so you code is end at fuction two – new_to_code Jul 29 '21 at 01:23
  • In function 2 add ```B.func5()``` at the end – new_to_code Jul 29 '21 at 01:25
  • 1
    The problem I have is that I wanted the functions of both classes to be used by each other unless it's actually impossible then I'll just have to figure out something else. Please continue to give more tips and advice, as I'll be looking at this topic as regularly as I can until I find a satisfying solution or an alternative method of getting the outcome. :) – AIzen Vermillion Jul 29 '21 at 02:34
  • Hi so you can put add the function is infinite loop so that the fuction keep runing and then when it is impossible break the loop – new_to_code Jul 30 '21 at 02:45
  • You can put the above in try and ex block so when ever there is an error the code will automatically remove it self from looop – new_to_code Jul 30 '21 at 02:47
  • Nice advice I'll try those. :) – AIzen Vermillion Jul 30 '21 at 07:41
  • 1
    This is a bit of a mess. Why are both classes dealing with global variables? Why are you using instance methods as if they were class or static methods? It's hard to follow the code since the method names make no reference to what they do or what their purpose is. – chepner Jul 31 '21 at 16:09
  • `c in DECK` is going to be true by definition, because you define `c` to be an element (the first, specifically) of `DECK`. – chepner Jul 31 '21 at 16:11
  • I think the key understanding that's missing is the difference between `start_game.func1()` and `A.func1()`. – Ulrich Eckhardt Jul 31 '21 at 16:13
  • I'd agree it does look a bit messy but what I was trying to do was to have two different classes call each others variables and functions and see whether it's possible or not. If it is possible how? and if not why and what alternative syntax and/or solution can I do that would let me do the result I want. :) – AIzen Vermillion Aug 16 '21 at 05:41

1 Answers1

0
import random

HAND = []
DECK = ['Ojama Red', 'Ojama Blue','Ojama Yellow','Ojama Green','Ojama Black', 'Ojama Pink', 'Ojama Lime']

class B:
    def func3(self):
        self.func4()
    def func4(self): # DRAW
        A.func2()
        self.func5()
    def func5(self): # MAIN PHASE 1
        print('\n', HAND)
        print('\n', DECK)

class A:
    def __init__(self):
        self.B = B()
    def func1(self, DECK):
        random.shuffle(DECK)
        self.B.func3()
    def func2(self):
        c = DECK[0]
        if c in DECK:
            HAND.append(c)
            DECK.remove(c) 



A = A()
A.func1(DECK)

This will give the output like.

['Ojama Lime']

['Ojama Yellow', 'Ojama Black', 'Ojama Blue', 'Ojama Pink', 'Ojama Red', 'Ojama Green']
>