下面這段代碼的問題是,除了列印陳述句之外,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)
This is the ```output``` I am receiving
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
uj5u.com熱心網友回復:
你拼錯了 checkwin 變數
改變這個:
return check_win
對此:
return checkwin
在第 84 行
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/418843.html
標籤:
上一篇:執行另一個函式時如何呼叫函式
