我是编程新手,还是python新手。我做了一些小项目(剪刀石头布和子手),没有重大问题。为了挑战自我,我试图连续进行4个游戏,而没有示例可作为基础。我创建了代表游戏某个步骤的多个功能。
功能(get_player_input
)之一负责用户输入。我要求用户选择一列。然后,我检查多件事(从1到7是一个int值,列是否未满?)。如果输入有效,则返回变量column_select
和free_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教程,其中介绍了如何将变量从一个函数重新调谐到另一个函数,但是我还没有找到如何处理这个问题的方法。我发现了一些可以解决我问题的方法,但是目前看来,没有一个方法可以解决:
我还写了一小段代码来消除一些复杂性。希望使自己更容易理解:
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] 删除。
我来说两句