3

How would I make a loop that creates multiple functions? I need to create multiple functions, with variables for the colX in the example below.

In other words, how would I create a loop to simply the following...

def game_col1(self):
    self.player = self.player + 1
    if self.player %2 == 0:
        self.window.col1_playera()
    else:
        self.window.col1_playerb()
    print(self.player)
def game_col2(self):
    self.player = self.player + 1
    if self.player %2 == 0:
        self.window.col2_playera()
    else:
        self.window.col2_playerb()
    print(self.player)
def game_col3(self):
    self.player = self.player + 1
    if self.player %2 == 0:
        self.window.col3_playera()
    else:
        self.window.col3_playerb()
    print(self.player)
def game_col4(self):
    self.player = self.player + 1
    if self.player %2 == 0:
        self.window.col4_playera()
    else:
        self.window.col4_playerb()
    print(self.player)
def game_col5(self):
    self.player = self.player + 1
    if self.player %2 == 0:
        self.window.col5_playera()
    else:
        self.window.col5_playerb()
    print(self.player)
def game_col6(self):
    self.player = self.player + 1
    if self.player %2 == 0:
        self.window.col6_playera()
    else:
        self.window.col6_playerb()
    print(self.player)
def game_col7(self):
    self.player = self.player + 1
    if self.player %2 == 0:
        self.window.col7_playera()
    else:
        self.window.col7_playerb()
    print(self.player)
2
  • Please have a look at sections 4.2 ("for Statements") and 4.7 ("More on Defining Functions") in the Python tutorial. It would probably be useful to look over the table of contents and read any sections you're not familiar with. Commented May 7, 2015 at 1:56
  • 4
    Why not just make a single game_col function that takes the number as an argument? Commented May 7, 2015 at 2:05

2 Answers 2

2

Without delving into the reasoning behind why you have written your code the way you have, I'm going to give you exactly what you've asked for, but be warned: it's ugly. Also, I'm assuming those functions game_col[1-7] are members of a class based on the self parameter.

class Game(object):
    def __init__(self):
        ...
        def game_col(s, i):
            s.player = s.player + 1
            if s.player % 2 == 0:
                eval('s.window.col%d_playera()' % i)
            else:
                eval('s.window.col%d_playerb()' % i)
            print(s.player)
        for i in range(8)[1:]:
            setattr(self, 'game_col%d' % i, lambda: game_col(self, i))

If you now declare a game object like so:

game = Game()

You can use the functions that you desire, like so:

game.game_col1()
game.game_col2()
...
game.game_col7()
Sign up to request clarification or add additional context in comments.

Comments

0

This, while not dynamically achieving what you're asking, will generate the code you desire:

for x in range(0, 99):
    print """def game_col{0}(self): 
    self.player = self.player + 1 
    if self.player %2 == 0: 
        self.window.col{1}_playera() 
    else: 
        self.window.col{2}_playerb() 
    print(self.player) \n""".format(x, x, x)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.