python - 终端未注册定义的功能
问题描述
我正在用python尝试一个简单的项目,它是创建一个井字游戏。下面,您将看到我的代码。当我将代码粘贴到终端时,我得到的错误消息是 display_board 函数未定义,但正如您从下面的代码中看到的那样,它是第一个定义的函数。我也包括了这些步骤。顺便说一句,虽然我不明白为什么,但我正在遵循的教程省略了第 2 步。任何人都可以帮忙吗?
#Imported modules
import random
# Global variables
theBoard = [' '] * 10 # a list of empty spaces
available = [str(num) for num in range(0,10)] # a List Comprehension
players = [0,'X','O']
#Step 1: Write a function that can print out a board. Set up your board as a list,
#where each index 1-9 corresponds with a number on a number pad,
# so you get a 3 by 3 board representation.
def display_board(a,b):
print('\n'*100)
print('Available TIC-TAC-TOE\n'+
' moves\n\n '+
a[7]+'|'+a[8]+'|'+a[9]+' '+b[7]+'|'+b[8]+'|'+b[9]+'\n '+
'-+-+- -+-+-\n '+
a[4]+'|'+a[5]+'|'+a[6]+' '+b[4]+'|'+b[5]+'|'+b[6]+'\n '+
'-+-+- -+-+-\n '+
a[1]+'|'+a[2]+'|'+a[3]+' '+b[1]+'|'+b[2]+'|'+b[3]+'\n')
display_board(available,theBoard)
#Step 2: Write a function that can take in a player input and assign their marker as 'X' or 'O'.
#Think about using while loops to continually ask until you get a correct answer.
def player_input():
marker = ''
while marker != 'X' and marker != 'O':
marker = input('Player 1: Do you want to be X or O? ').upper()
if marker == 'X':
return ('X', 'O')
else:
return ('O', 'X')
player1_marker, player2_marker = player_input()
#Step 3: Write a function that takes in the board list object, a marker ('X' or 'O'),
# and a desired position (number 1-9) and assigns it to the board.
def place_marker(avail,board,marker,position):
board[position] = marker
avail[position] = ' '
#Step 4: Write a function that takes in a board and a mark (X or O) and then checks to see if that mark has won.
def win_check(board,mark):
return
((board[7] == board[8] == board[9] == mark) or # across the top
(board[4] == board[5] == board[6] == mark) or # across the middle
(board[1] == board[2] == board[3] == mark) or # across the bottom
(board[7] == board[4] == board[1] == mark) or # down the left
(board[8] == board[5] == board[2] == mark) or # down the middle
(board[9] == board[6] == board[3] == mark) or # down the right
(board[7] == board[5] == board[3] == mark) or # diagonal
(board[9] == board[5] == board[1] == mark)) # diagonal
#Step 5: Write a function that uses the random module to randomly decide which player goes first.
def random_player():
return random.choice((-1, 1))
#Step 6: Write a function that returns a boolean indicating whether a space on the board is freely available.
def space_check(board,position):
return board[position] == ' ' #bool to check the position between 0,10
#Step 7: Write a function that checks if the board is full and returns a boolean value. True if full, False otherwise.
def full_board_check(board):
return ' ' not in board[1:]
#Step 8: Write a function that asks for a player's next position (as a number 1-9)
#and then uses the function from step 6 to check if it's a free position.
# If it is, then return the position for later use.
def player_choice(board,player):
position = 0
while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position):
try:
position = int(input('Player %s, choose your next position: (1-9) '%(player)))
except:
print("I'm sorry, please try again.")
return position
#Step 9: Write a function that asks the player if they want to play again and returns a boolean True if they do want to play again.
def replay():
return input('Do you want to play again? Enter Yes or No: ').lower().startswith('y')
#Run the game
while True:
print('\n'*100)
toggle = random_player()
player = players[toggle]
print('Welcome to Tic Tac Toe!')
print('For this round, Player %s will go first!' %(player))
game_on = True
input('Hit Enter to continue')
while game_on:
display_board(available,theBoard)
position = player_choice(theBoard,player)
place_marker(available,theBoard,player,position)
if win_check(theBoard, player):
display_board(available,theBoard)
print('Congratulations! Player '+player+' wins!')
game_on = False
else:
if full_board_check(theBoard):
display_board(available,theBoard)
print('The game is a draw!')
break
else:
toggle *= -1
player = players[toggle]
print('\n'*100)
# reset the board and available moves list
theBoard = [' '] * 10
available = [str(num) for num in range(0,10)]
if not replay():
break
解决方案
我认为您以错误的方式运行该项目。将此代码复制到扩展名为 py 的文件中,然后在文件目录中打开终端并键入“python file.py”
推荐阅读
- mongodb - 如果未找到结果,则返回 null 默认值
- angular - Angular 6:将订阅数据分配给局部变量以进行进一步处理
- c# - 如何在 Outlook 插件中访问 EWS `UniqueId` 字段
- javascript - 在同一页面中创建多个图表时出现问题
- assembly - 我的 x86 8086 的 ASM 程序打印不好
- php - 从数据库中获取数据并使用php将其显示在表中
- javascript - 在没有 JavaScript 上传的情况下显示文件上传进度
- javascript - 如何将平面数据结构转换为树状结构?
- html - 如何改变1个句子的颜色
标签而不添加新行?
- php - 如何将字符串中的两个字符视为一个字节?