xray911
xray911

Reputation: 31

Python: Return variables from one function to the other without rerunning the first function

I'm new to programming and new to python. I have made some small projects (rock-paper-scissor and hangman) without major issues. In an attempt to challenge myself I'm trying to make a game of 4 in a row without an example to base it of. I have created multiple functions which represent a piece/step of the game.

One of the functions (get_player_input) takes care of the user input. I ask the user to select a column. I then check multiple things (is it a int from 1 till 7 and is the column not full?). If the input is valid, I return the variables column_select and free_places_column. The reason I return these variables is because I want to reuse this information to "place a piece" on the gameboard with the second function (place_piece)

This is where I get lost. I'm able to use these variables with use of: column_select, free_places_column = get_player_input() However this piece of code reruns the function get_player_input. Resulting in the user being asked twice in which column he want to put a piece.

My code thusfar:

# The game 4 in a row

# Define the game table with 6 rows and 7 columns
game_board = [[" ", " ", " ", " ", " ", " ", " "],
              [" ", " ", " ", " ", " ", " ", " "],
              [" ", " ", " ", " ", " ", " ", " "],
              [" ", " ", " ", " ", " ", " ", " "],
              [" ", " ", " ", " ", " ", " ", " "],
              [" ", " ", " ", " ", " ", " ", " "]]


# print the game board
def printboard():
    print("|", game_board[0][0], "|", game_board[0][1], "|", game_board[0][2], "|", game_board[0][3], "|",
          game_board[0][4], "|", game_board[0][5], "|", game_board[0][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("|", game_board[1][0], "|", game_board[1][1], "|", game_board[1][2], "|", game_board[1][3], "|",
          game_board[1][4], "|", game_board[1][5], "|", game_board[1][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("|", game_board[2][0], "|", game_board[2][1], "|", game_board[2][2], "|", game_board[2][3], "|",
          game_board[2][4], "|", game_board[2][5], "|", game_board[2][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("|", game_board[3][0], "|", game_board[3][1], "|", game_board[3][2], "|", game_board[3][3], "|",
          game_board[3][4], "|", game_board[3][5], "|", game_board[3][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("|", game_board[4][0], "|", game_board[4][1], "|", game_board[4][2], "|", game_board[4][3], "|",
          game_board[4][4], "|", game_board[4][5], "|", game_board[4][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("|", game_board[5][0], "|", game_board[5][1], "|", game_board[5][2], "|", game_board[5][3], "|",
          game_board[5][4], "|", game_board[5][5], "|", game_board[5][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("  1   2   3   4   5   6   7")
    print()



def get_player_input():
    # set varaibles
    free_places_column = 0
    received_valid_input = False

    # Validate if player input is int and anywhere from 1 to 7. if not ask again
    while received_valid_input == False:
        try:
            column_select = int(input("Which column (1-7) do you want to drop the piece: "))
            if 0 < column_select < 8:
                for i in range(0, 6):
                    if game_board[i][column_select - 1] == " ":
                        free_places_column = free_places_column + 1
                if free_places_column == 0:
                    print("Column is full. please select an other column")
                else:
                    received_valid_input = True
            else:
                print('Please provide a number between 1 and 7')
        except ValueError:
            print('Wrong input. Please enter a number between 1 and 7')
        return column_select, free_places_column

def place_piece(player):
    column_select, free_places_column = get_player_input()
    print("Going to place a piece in column", column_select, "and row", free_places_column)
    if player == "A":
        game_board[free_places_column - 1][column_select - 1] = "X"
    else:
        game_board[free_places_column - 1][column_select - 1] = "O"


while True:
    printboard()
    get_player_input()
    place_piece("A")

When I run this code this is the result:

|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
  1   2   3   4   5   6   7

Which column (1-7) do you want to drop the piece: 1
Which column (1-7) do you want to drop the piece: 1
Going to place a piece in column 1 and row 6
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
| X |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
  1   2   3   4   5   6   7

I have searched the internet and watched YouTube tutorials on retuning variables from one function to the other but I have not yet found how to deal with this. I found some this which could solve my issues but none of them seem the way to go for now:

I also wrote a small piece of code to remove some of the complexity. Hopefully making it easier to understand for myself:

def function1():
    a = 10
    b = 20
    print("We are in function 1")
    return a, b


def function2():
    a, b = function1()
    print(a, b)
    print("We are in function 2")


function1()
function2()

Result:

We are in function 1
We are in function 1
10 20
We are in function 2

Can anybody point me in the right direction?

Best regards

Upvotes: 1

Views: 103

Answers (1)

Synthaze
Synthaze

Reputation: 6090

# The game 4 in a row

# Define the game table with 6 rows and 7 columns
game_board = [[" ", " ", " ", " ", " ", " ", " "],
              [" ", " ", " ", " ", " ", " ", " "],
              [" ", " ", " ", " ", " ", " ", " "],
              [" ", " ", " ", " ", " ", " ", " "],
              [" ", " ", " ", " ", " ", " ", " "],
              [" ", " ", " ", " ", " ", " ", " "]]


# print the game board
def printboard():
    print("|", game_board[0][0], "|", game_board[0][1], "|", game_board[0][2], "|", game_board[0][3], "|",
          game_board[0][4], "|", game_board[0][5], "|", game_board[0][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("|", game_board[1][0], "|", game_board[1][1], "|", game_board[1][2], "|", game_board[1][3], "|",
          game_board[1][4], "|", game_board[1][5], "|", game_board[1][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("|", game_board[2][0], "|", game_board[2][1], "|", game_board[2][2], "|", game_board[2][3], "|",
          game_board[2][4], "|", game_board[2][5], "|", game_board[2][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("|", game_board[3][0], "|", game_board[3][1], "|", game_board[3][2], "|", game_board[3][3], "|",
          game_board[3][4], "|", game_board[3][5], "|", game_board[3][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("|", game_board[4][0], "|", game_board[4][1], "|", game_board[4][2], "|", game_board[4][3], "|",
          game_board[4][4], "|", game_board[4][5], "|", game_board[4][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("|", game_board[5][0], "|", game_board[5][1], "|", game_board[5][2], "|", game_board[5][3], "|",
          game_board[5][4], "|", game_board[5][5], "|", game_board[5][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("  1   2   3   4   5   6   7")
    print()



def get_player_input():
    # set varaibles
    free_places_column = 0
    received_valid_input = False

    # Validate if player input is int and anywhere from 1 to 7. if not ask again
    while received_valid_input == False:
        try:
            column_select = int(input("Which column (1-7) do you want to drop the piece: "))
            if 0 < column_select < 8:
                for i in range(0, 6):
                    if game_board[i][column_select - 1] == " ":
                        free_places_column = free_places_column + 1
                if free_places_column == 0:
                    print("Column is full. please select an other column")
                else:
                    received_valid_input = True
            else:
                print('Please provide a number between 1 and 7')
        except ValueError:
            print('Wrong input. Please enter a number between 1 and 7')
        return column_select, free_places_column

def place_piece(player):
    column_select, free_places_column = get_player_input()
    print("Going to place a piece in column", column_select, "and row", free_places_column)
    if player == "A":
        game_board[free_places_column - 1][column_select - 1] = "X"
    else:
        game_board[free_places_column - 1][column_select - 1] = "O"
   

while True:
    printboard()
    place_piece("A")

I just removed one line in your while True loop.

As you call get_player_input() in place_piece(player) function, there is noneed to call it twice.

You want both functions to be executed in a given order, but since the get_player_input() is embedded in the code of place_piece(player), you just need to execute place_piece(player).

To use your simple example:

def function1():
    a = 10
    b = 20
    print("We are in function 1")
    return a, b


def function2():
    a, b = function1()
    print(a, b)
    print("We are in function 2")

If you call only function2(), the ouput is:

We are in function 1
10 20
We are in function 2

If you call only function1(), the output is:

We are in function 1

If you call function1() and then function2(), the output is:

We are in function 1
We are in function 1
10 20
We are in function 2

And function2() and then function1():

We are in function 1
10 20
We are in function 2
We are in function 1

To get what you want by calling two functions in your code, you need:

def function1():
    a = 10
    b = 20
    print("We are in function 1")
    return a, b


def function2(a,b):
    print(a, b)
    print("We are in function 2")

a, b = function1()

function2(a,b)

And you get:

We are in function 1
10 20
We are in function 2

With your full code:

# The game 4 in a row

# Define the game table with 6 rows and 7 columns
game_board = [[" ", " ", " ", " ", " ", " ", " "],
              [" ", " ", " ", " ", " ", " ", " "],
              [" ", " ", " ", " ", " ", " ", " "],
              [" ", " ", " ", " ", " ", " ", " "],
              [" ", " ", " ", " ", " ", " ", " "],
              [" ", " ", " ", " ", " ", " ", " "]]


# print the game board
def printboard():
    print("|", game_board[0][0], "|", game_board[0][1], "|", game_board[0][2], "|", game_board[0][3], "|",
          game_board[0][4], "|", game_board[0][5], "|", game_board[0][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("|", game_board[1][0], "|", game_board[1][1], "|", game_board[1][2], "|", game_board[1][3], "|",
          game_board[1][4], "|", game_board[1][5], "|", game_board[1][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("|", game_board[2][0], "|", game_board[2][1], "|", game_board[2][2], "|", game_board[2][3], "|",
          game_board[2][4], "|", game_board[2][5], "|", game_board[2][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("|", game_board[3][0], "|", game_board[3][1], "|", game_board[3][2], "|", game_board[3][3], "|",
          game_board[3][4], "|", game_board[3][5], "|", game_board[3][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("|", game_board[4][0], "|", game_board[4][1], "|", game_board[4][2], "|", game_board[4][3], "|",
          game_board[4][4], "|", game_board[4][5], "|", game_board[4][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("|", game_board[5][0], "|", game_board[5][1], "|", game_board[5][2], "|", game_board[5][3], "|",
          game_board[5][4], "|", game_board[5][5], "|", game_board[5][6], "|")
    print("- - + - + - + - + - + - + - -")
    print("  1   2   3   4   5   6   7")
    print()



def get_player_input():
    # set varaibles
    free_places_column = 0
    received_valid_input = False

    # Validate if player input is int and anywhere from 1 to 7. if not ask again
    while received_valid_input == False:
        try:
            column_select = int(input("Which column (1-7) do you want to drop the piece: "))
            if 0 < column_select < 8:
                for i in range(0, 6):
                    if game_board[i][column_select - 1] == " ":
                        free_places_column = free_places_column + 1
                if free_places_column == 0:
                    print("Column is full. please select an other column")
                else:
                    received_valid_input = True
            else:
                print('Please provide a number between 1 and 7')
        except ValueError:
            print('Wrong input. Please enter a number between 1 and 7')
        return column_select, free_places_column

def place_piece(player,column_select,free_places_column):
    print("Going to place a piece in column", column_select, "and row", free_places_column)
    if player == "A":
        game_board[free_places_column - 1][column_select - 1] = "X"
    else:
        game_board[free_places_column - 1][column_select - 1] = "O"
   

while True:
    printboard()
    column_select, free_places_column = get_player_input()
    place_piece("A",column_select, free_places_column)

Output:

Which column (1-7) do you want to drop the piece: 2
Going to place a piece in column 2 and row 6
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   | X |   |   |   |   |
- - + - + - + - + - + - + - -
| X | X | X |   |   |   |   |
- - + - + - + - + - + - + - -
  1   2   3   4   5   6   7

Which column (1-7) do you want to drop the piece: 3
Going to place a piece in column 3 and row 4
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   | X |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   | X |   |   |   |   |
- - + - + - + - + - + - + - -
| X | X | X |   |   |   |   |
- - + - + - + - + - + - + - -
  1   2   3   4   5   6   7

Which column (1-7) do you want to drop the piece: 

Upvotes: 1

Related Questions