Python:将变量从一个函数返回到另一个函数,而无需重新运行第一个函数

X射线911

我是编程新手,还是python新手。我做了一些小项目(剪刀石头布和子手),没有重大问题。为了挑战自我,我试图连续进行4个游戏,而没有示例可作为基础。我创建了代表游戏某个步骤的多个功能。

功能(get_player_input)之一负责用户输入。我要求用户选择一列。然后,我检查多件事(从1到7是一个int值,列是否未满?)。如果输入有效,则返回变量column_selectfree_places_column我返回这些变量的原因是因为我想使用第二个函数(place_piece来重用此信息以在游戏板上“放置一块”

这是我迷路的地方。我可以通过以下方式使用这些变量:column_select, free_places_column = get_player_input()但是,这段代码重新运行了该函数get_player_input结果导致用户被问了两次,他想在哪一列中放一块。

到目前为止,我的代码:

# 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")

当我运行此代码时,结果如下:

|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
|   |   |   |   |   |   |   |
- - + - + - + - + - + - + - -
  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

我已经在互联网上搜索并观看了YouTube教程,其中介绍了如何将变量从一个函数重新调谐到另一个函数,但是我还没有找到如何处理这个问题的方法。我发现了一些可以解决我问题的方法,但是目前看来,没有一个方法可以解决:

  • 使我的变量全局化->发现了很多人告诉其他人不要这样做
  • 开始使用类->对我来说仍然是一个很难理解的概念。还有一种想解决这个问题而又不完全摆脱迄今为止的选择
  • 抛弃函数并编写1条大代码->尝试学习使用函数

我还写了一小段代码来消除一些复杂性。希望使自己更容易理解:

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()

结果:

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

有人能指出我正确的方向吗?

最好的祝福

合酶
# 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")

我只是在您的True循环中删除了一行。

正如你所说get_player_input()place_piece(player)功能,有noneed两次调用它。

您希望两个函数都以给定的顺序执行,但是由于get_player_input()嵌入在的代码中place_piece(player),因此只需要执行即可place_piece(player)

使用简单的示例:

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")

如果仅致电function2(),则输出为:

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

如果仅调用function1(),则输出为:

We are in function 1

如果您function1()依次致电,则function2()输出为:

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

而且function2(),然后function1()

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

要通过在代码中调用两个函数来获得所需的内容,您需要:

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)

你会得到:

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

使用完整的代码:

# 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)

输出:

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: 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在powershell中将变量从一个函数返回到另一个函数

来自分类Dev

将布尔值从一个函数返回到另一个函数-VBA

来自分类Dev

如何将字符串从一个函数返回到另一个函数?

来自分类Dev

将变量从一个函数传递到另一个函数-Javascript

来自分类Dev

将变量从一个函数传递到另一个函数

来自分类Dev

将变量从一个 JS 函数传递到另一个函数

来自分类Dev

将变量 (id) 从一个函数传递到另一个函数

来自分类Dev

在Python中从一个函数调用变量到另一个函数

来自分类Dev

将一个函数中的局部变量返回给另一个函数

来自分类Dev

将javascript变量从一个函数/文件传递到另一个

来自分类Dev

我想在另一个函数中使用一个函数的返回值,而无需再次运行该函数。(蟒蛇)

来自分类Dev

另一个函数从一个函数访问返回的值

来自分类Dev

从一个函数返回的数组没有被另一个函数捕获

来自分类Dev

从一个函数传递操纵变量以在另一个函数中使用

来自分类Dev

C ++如何使用从一个函数到另一个函数的变量?

来自分类Dev

从一个函数调用变量到另一个函数

来自分类Dev

在JavaScript中从一个函数访问变量到另一个函数

来自分类Dev

Python 函数将类型作为参数返回到另一个脚本

来自分类Dev

如何通过多处理将布尔值从一个函数返回到另一个函数?

来自分类Dev

将输出从一个函数链接到另一个函数?

来自分类Dev

将值从一个函数传递到另一个函数

来自分类Dev

将结果从一个函数拉到另一个函数

来自分类Dev

Swift 将数据从一个函数传递到另一个函数

来自分类Dev

无法将链接从一个函数传递到另一个函数

来自分类Dev

将函数的静态数组返回到main()函数中的另一个数组

来自分类Dev

函数将数组返回到另一个函数>未捕获的TypeError

来自分类Dev

在python中从一个函数到另一个函数传输列表

来自分类Dev

Python:如何从一个函数调用另一个函数的属性?

来自分类Dev

使用ProcessingJS将返回值从一个函数传递到另一个函数的语法?

Related 相关文章

  1. 1

    在powershell中将变量从一个函数返回到另一个函数

  2. 2

    将布尔值从一个函数返回到另一个函数-VBA

  3. 3

    如何将字符串从一个函数返回到另一个函数?

  4. 4

    将变量从一个函数传递到另一个函数-Javascript

  5. 5

    将变量从一个函数传递到另一个函数

  6. 6

    将变量从一个 JS 函数传递到另一个函数

  7. 7

    将变量 (id) 从一个函数传递到另一个函数

  8. 8

    在Python中从一个函数调用变量到另一个函数

  9. 9

    将一个函数中的局部变量返回给另一个函数

  10. 10

    将javascript变量从一个函数/文件传递到另一个

  11. 11

    我想在另一个函数中使用一个函数的返回值,而无需再次运行该函数。(蟒蛇)

  12. 12

    另一个函数从一个函数访问返回的值

  13. 13

    从一个函数返回的数组没有被另一个函数捕获

  14. 14

    从一个函数传递操纵变量以在另一个函数中使用

  15. 15

    C ++如何使用从一个函数到另一个函数的变量?

  16. 16

    从一个函数调用变量到另一个函数

  17. 17

    在JavaScript中从一个函数访问变量到另一个函数

  18. 18

    Python 函数将类型作为参数返回到另一个脚本

  19. 19

    如何通过多处理将布尔值从一个函数返回到另一个函数?

  20. 20

    将输出从一个函数链接到另一个函数?

  21. 21

    将值从一个函数传递到另一个函数

  22. 22

    将结果从一个函数拉到另一个函数

  23. 23

    Swift 将数据从一个函数传递到另一个函数

  24. 24

    无法将链接从一个函数传递到另一个函数

  25. 25

    将函数的静态数组返回到main()函数中的另一个数组

  26. 26

    函数将数组返回到另一个函数>未捕获的TypeError

  27. 27

    在python中从一个函数到另一个函数传输列表

  28. 28

    Python:如何从一个函数调用另一个函数的属性?

  29. 29

    使用ProcessingJS将返回值从一个函数传递到另一个函数的语法?

热门标签

归档