【问题标题】:Python function not returning anything - Tic Tac ToePython 函数不返回任何内容 - Tic Tac Toe
【发布时间】:2022-01-21 07:16:21
【问题描述】:

下面这段代码的问题是,除了打印语句之外,check_win() 函数没有返回任何内容,我可以通过放弃 while 循环中的所有逻辑而不使用函数来解决这个问题,但我希望这段代码是水晶的清楚,有什么建议吗?

board = {
    'a1': '', 'a2': '', 'a3': '',
    'b1': '', 'b2': '', 'b3': '',
    'c1': '', 'c2': '', 'c3': ''
}

player_one = True

player_one_choice = input("Play player one: ")
if player_one_choice in board:
    board[player_one_choice] = 'x'
    player_one = False

elif player_one_choice not in board:
    print("Wrong input")
print(board)

checkwin = False


def check_win():
    """
    'x' check win logic
    """
    # Vertical check logic
    if 'x' in board['a1'] and 'x' in board['a2'] and 'x' in board['a3']:
        print("congrats x won")
        checkwin == True
    elif 'x' in board['b1'] and 'x' in board['b2'] and 'x' in board['b3']:
        print("congrats x won")
        checkwin == True
    elif 'x' in board['c1'] and 'x' in board['c2'] and 'x' in board['c3']:
        print("congrats x won")
        checkwin == True

    # Horizontal check logic
    elif 'x' in board['a1'] and 'x' in board['b1'] and 'x' in board['c1']:
        print("congrats x won")
        checkwin == True
    elif 'x' in board['a2'] and 'x' in board['b2'] and 'x' in board['c2']:
        print("congrats x won")
        checkwin == True
    elif 'x' in board['a3'] and 'x' in board['b3'] and 'x' in board['c3']:
        print("congrats x won")
        checkwin == True

    # Diagonal check logic
    elif 'x' in board['a1'] and 'x' in board['b2'] and 'x' in board['c3']:
        print("congrats x won")
        checkwin == True
    elif 'x' in board['a3'] and 'x' in board['b2'] and 'x' in board['c1']:
        print("congrats x won")
        checkwin == True

    elif 'o' in board['a1'] and 'o' in board['a2'] and 'o' in board['a3']:
        print("congrats o won")
        checkwin == True
    elif 'o' in board['b1'] and 'o' in board['b2'] and 'o' in board['b3']:
        print("congrats o won")
        checkwin == True
    elif 'o' in board['c1'] and 'o' in board['c2'] and 'o' in board['c3']:
        print("congrats o won")
        checkwin == True

    # Horizontal check logic
    elif 'o' in board['a1'] and 'o' in board['b1'] and 'o' in board['c1']:
        print("congrats o won")
        checkwin == True
    elif 'o' in board['a2'] and 'o' in board['b2'] and 'o' in board['c2']:
        print("congrats o won")
        checkwin == True
    elif 'o' in board['a3'] and 'o' in board['b3'] and 'o' in board['c3']:
        print("congrats o won")
        checkwin == True

    # Diagonal check logic
    elif 'o' in board['a1'] and 'o' in board['b2'] and 'o' in board['c3']:
        print("congrats o won")
        checkwin == True
    elif 'o' in board['a3'] and 'o' in board['b2'] and 'o' in board['c1']:
        print("congrats o won")
        checkwin == True

    return check_win


while True:
    check_win()

    player_two_choice = input("PLay player two: ")

    if player_two_choice != 'x' and player_one_choice in board:
        board[player_two_choice] = 'o'
        player_one = True
        print(board)
    if player_two_choice != 'x' and player_one_choice not in board or player_two_choice == 'a' or player_two_choice == 'b' or player_two_choice == 'c':
        print("Wrong input")

    check_win()

    if checkwin:
        break

    player_one_choice = input("Play player one: ")

    if player_one_choice in board:
        board[player_one_choice] = 'x'
        player_one = False

    elif player_one_choice not in board:
        print("Wrong input")
        break

    print(board)

这是我收到的“输出”


>>>Play player one: a1
{'a1': 'x', 'a2': '', 'a3': '', 'b1': '', 'b2': '', 'b3': '', 'c1': '', 'c2': '', 'c3': ''}
>>>PLay player two: b1
{'a1': 'x', 'a2': '', 'a3': '', 'b1': 'o', 'b2': '', 'b3': '', 'c1': '', 'c2': '', 'c3': ''}
>>>Play player one: a2
{'a1': 'x', 'a2': 'x', 'a3': '', 'b1': 'o', 'b2': '', 'b3': '', 'c1': '', 'c2': '', 'c3': ''}
>>>PLay player two: b2
{'a1': 'x', 'a2': 'x', 'a3': '', 'b1': 'o', 'b2': 'o', 'b3': '', 'c1': '', 'c2': '', 'c3': ''}
>>>Play player one: a3
{'a1': 'x', 'a2': 'x', 'a3': 'x', 'b1': 'o', 'b2': 'o', 'b3': '', 'c1': '', 'c2': '', 'c3': ''}
congrats x won
>>>PLay player two: b3
{'a1': 'x', 'a2': 'x', 'a3': 'x', 'b1': 'o', 'b2': 'o', 'b3': 'o', 'c1': '', 'c2': '', 'c3': ''}
congrats x won
>>>Play player one: 
Wrong input
    
Process finished with exit code 0

【问题讨论】:

  • checkwin 函数中的变量 check_win() 是一个局部变量。更改它对您的全局变量check_win 没有任何影响。你需要在你的程序中添加一个global check_win(如果你唯一的意图是改变全局变量的值,你可以去掉底部的return check_win
  • 你怎么能说check_win() 没有返回任何东西?您永远不会存储或查看它返回的内容。顺便说一句,所有'x' in board[...] 表达式都应该是'x' == board[...]
  • @FrankYellin 的解释是正确的,但是添加global 的建议不好。是的,它会起作用,但最好将函数内部的check_win初始化为False,然后在调用函数checkwin = check_win()时将返回值绑定到一个名称
  • @TimRoberts 感谢您的建议,但是在和 == 中做同样的事情我看不出有什么不同,@buran 你说的checkwin = check_win()是什么意思@
  • checkwin == True 是什么意思? checkwin = True?

标签: python function while-loop boolean


【解决方案1】:

你拼错了 checkwin 变量

改变这个:

return check_win

到这里:

return checkwin

第 84 行

【讨论】:

  • 这是问题之一。即使他解决了这个问题,它也不起作用,因为他不使用 check_win 返回的内容。
  • UnboundLocalError: local variable 'checkwin' referenced before assignment
  • 当我将return check_win 更改为return checkwin 时,程序返回的就是这个
  • 那么我应该将checkwin设为全局吗?
猜你喜欢
  • 2018-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-16
相关资源
最近更新 更多