[英]How to use a return value from a function in a completely different function
I am writing a tic-tac-toe game and currently have the following functions: 我正在写一个tic-tac-toe游戏,目前有以下功能:
# Need: Board, Display Board, Play Game, Alternating Terms, Os, Xs, Wins, Losses
board = ["-", "-", "-",
"-", "-", "-",
"-", "-", "-", ]
def display_board():
print(board[0] + " | " + board[1] + " | " + board[2])
print(board[3] + " | " + board[4] + " | " + board[5])
print(board[6] + " | " + board[7] + " | " + board[8])
def handle_turn():
position = input("\nChoose a position from 1 to 9: ")
position = int(position) - 1
return position
def choose_player_marker(marker):
player_marker = ''
if marker == 'X' or marker == 'x':
player_marker = 'X'
return player_marker
elif marker == 'O' or marker == 'o':
player_marker = 'O'
return player_marker
def opp_move():
opp_marker = ''
if choose_player_marker() == 'X':
opp_marker = 'O'
else:
opp_marker = 'X'
def play_game():
game_current = True
display_board()
marker = input("\nPlease choose a marker: 'X' or 'x' OR 'O' or 'o' : ")
choose_player_marker(marker)
opp_move()
handle_turn()
play_game()
In the function def choose_player_marker(marker):
I return player_marker = 'X'
or player_marker = 'O'
, depending on the condition. 在函数
def choose_player_marker(marker):
我返回player_marker = 'X'
或player_marker = 'O'
,具体取决于条件。
I would like to use the resulting 'X'
or 'O'
in the function def opp_move():
to determine which marker the opponent will choose. 我想在函数
def opp_move():
使用结果'X'
或'O'
来确定对手将选择哪个标记。 However, I am having difficulty in properly delegating the return statement. 但是,我很难正确委派return语句。 I tried using
marker
as the argument in the def opp_move():
function and also have tried if choose_player_marker(marker) == 'X'
to no avail. 我尝试使用
marker
作为def opp_move():
函数中的参数,并尝试if choose_player_marker(marker) == 'X'
无效。
How do I correctly handle the return statement from def choose_player_marker(marker):
so that my def opp_move():
function will be able to determine what marker the opponent will choose? 如何正确处理
def choose_player_marker(marker):
的return语句def choose_player_marker(marker):
这样我的def opp_move():
函数将能够确定对手将选择哪个标记?
Now we can see your full code, the solution is simple. 现在我们可以看到您的完整代码,解决方案很简单。
You already call choose_player_marker()
in main
. 你已经在
main
调用了choose_player_marker()
。 You don't need to call it in opp_move
as well. 你也不需要在
opp_move
调用它。 What you need to do instead is to capture the result when you call it in main, and then pass in the value to opp_move: 你需要做的是在main中调用它时捕获结果,然后将值传递给opp_move:
def opp_move(marker):
if marker == 'X':
opp_marker = 'O'
else:
opp_marker = 'X'
return opp_marker
def play_game():
game_current = True
display_board()
marker = input("\nPlease choose a marker: 'X' or 'x' OR 'O' or 'o' : ")
marker = choose_player_marker(marker)
opp_move(marker)
handle_turn()
Now there are various other things wrong with this code, like the fact that handle_turn doesn't do anything useful, and that as others have pointed out choose_player_marker can be trivially replaced with a call to marker.upper()
, but at least this should get you going. 现在这个代码还有其他各种错误,例如handle_turn没有做任何有用的事实,而且正如其他人指出的那样,choose_player_marker可以通过调用
marker.upper()
来简单地替换,但至少这应该是让你去
The function choose_player_marker
doesn't do anything useful at present. 函数
choose_player_marker
目前没有做任何有用的事情。 If you give it an X
it gives you back and X
, if you give it an O
it gives you back an O
. 如果你给它一个
X
它会让你回来和X
,如果你给它一个O
它会给你一个O
The only use it has right now is converting lowercase x and o to uppercase X and O. 它现在唯一的用途是将小写的x和o转换为大写的X和O.
Thus there is no need to call it, because if we know the input (X) then we know the output (X). 因此,不需要调用它,因为如果我们知道输入(X),那么我们就知道输出(X)。
Given the name: choose_player_marker
I suspect you want either a different name (eg normalise_player_marker()
or a different implementation, ie one that prompts for user-input asking the person if they want to be X or O?) 鉴于名称:
choose_player_marker
我怀疑你想要一个不同的名字(例如normalise_player_marker()
或不同的实现,即提示用户输入询问该人是否想要成为X或O?)
As for opp_move : There's no need to call choose_player_marker
. 至于opp_move:没有必要调用
choose_player_marker
。 If you know the player is X
then you know the opponent is O
, and vice versa. 如果你知道玩家是
X
那么你知道对手是O
,反之亦然。 Thus I suspect you want something like this: 因此,我怀疑你想要这样的东西:
def normalise_marker(marker):
if marker == 'X' or marker == 'x':
return 'X'
elif marker == 'O' or marker == 'o':
return 'O'
def opp_move(player_marker):
opp_marker = ''
normed_marker = normalise_marker(player_marker)
if normed_marker == 'X':
opp_marker = 'O'
else:
opp_marker = 'X'
# use opp_marker here
Note that in its current state normalise_marker doesn't handle the case of marker NOT being an X or O, and can be reduced to an even simpler implementation, but that is left as an exercise for you :) 请注意,在其当前状态下,normalise_marker不处理标记NOT为X或O的情况,并且可以简化为更简单的实现,但这仍然是一个练习:)
Im not familiar with Python, but I guess it would make it more simple if the provided argument was set to lower case, the additional check for Capital case wouldn't be needed. 我不熟悉Python,但我想如果将提供的参数设置为小写,它将使其更简单,不需要额外检查Capital案例。 Still You could probably shorten the code this way.
你仍然可以通过这种方式缩短代码。
def choose_player_marker(marker):
return marker.lower()
def opp_move(result):
if result == 'x':
return 'o'
else:
return 'x'
playerMarker = 'X'
res = choose_player_marker(playerMarker)
opp = opp_move(res)
print(result, opp)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.