Debugging 位置参数未定义
我正在做一个更大的项目来编写代码,这样用户就可以在电脑上玩Connect 4了。现在,用户可以选择是否先走,然后绘制电路板。在进行truing以确保用户只能输入合法移动时,我遇到了一个问题,即我的函数legal_moves()接受1个位置参数,并给出了0,但我不明白要达成一致需要做什么Debugging 位置参数未定义,debugging,python-3.x,Debugging,Python 3.x,我正在做一个更大的项目来编写代码,这样用户就可以在电脑上玩Connect 4了。现在,用户可以选择是否先走,然后绘制电路板。在进行truing以确保用户只能输入合法移动时,我遇到了一个问题,即我的函数legal_moves()接受1个位置参数,并给出了0,但我不明白要达成一致需要做什么 #connect 4 #using my own formating import random #define global variables X = "X" O = "O" EMPTY = "_" TI
#connect 4
#using my own formating
import random
#define global variables
X = "X"
O = "O"
EMPTY = "_"
TIE = "TIE"
NUM_ROWS = 6
NUM_COLS = 8
def display_instruct():
"""Display game instructions."""
print(
"""
Welcome to the second greatest intellectual challenge of all time: Connect4.
This will be a showdown between your human brain and my silicon processor.
You will make your move known by entering a column number, 1 - 7. Your move
(if that column isn't already filled) will move to the lowest available position.
Prepare yourself, human. May the Schwartz be with you! \n
"""
)
def ask_yes_no(question):
"""Ask a yes or no question."""
response = None
while response not in ("y", "n"):
response = input(question).lower()
return response
def ask_number(question,low,high):
"""Ask for a number within range."""
#using range in Python sense-i.e., to ask for
#a number between 1 and 7, call ask_number with low=1, high=8
low=1
high=NUM_COLS
response = None
while response not in range (low,high):
response=int(input(question))
return response
def pieces():
"""Determine if player or computer goes first."""
go_first = ask_yes_no("Do you require the first move? (y/n): ")
if go_first == "y":
print("\nThen take the first move. You will need it.")
human = X
computer = O
else:
print("\nYour bravery will be your undoing... I will go first.")
computer = X
human = O
return computer, human
def new_board():
board = []
for x in range (NUM_COLS):
board.append([" "]*NUM_ROWS)
return board
def display_board(board):
"""Display game board on screen."""
for r in range(NUM_ROWS):
print_row(board,r)
print("\n")
def print_row(board, num):
"""Print specified row from current board"""
this_row = board[num]
print("\n\t| ", this_row[num], "|", this_row[num], "|", this_row[num], "|", this_row[num], "|", this_row[num], "|", this_row[num], "|", this_row[num],"|")
print("\t", "|---|---|---|---|---|---|---|")
# everything works up to here!
def legal_moves(board):
"""Create list of column numbers where a player can drop piece"""
legals = []
if move < NUM_COLS: # make sure this is a legal column
for r in range(NUM_ROWS):
legals.append(board[move])
return legals #returns a list of legal columns
#in human_move function, move input must be in legal_moves list
print (legals)
def human_move(board,human):
"""Get human move"""
legals = legal_moves(board)
print("LEGALS:", legals)
move = None
while move not in legals:
move = ask_number("Which column will you move to? (1-7):", 1, NUM_COLS)
if move not in legals:
print("\nThat column is already full, nerdling. Choose another.\n")
print("Human moving to column", move)
return move #return the column number chosen by user
def get_move_row(turn,move):
move=ask_number("Which column would you like to drop a piece?")
for m in range (NUM_COLS):
place_piece(turn,move)
display_board()
def place_piece(turn,move):
if this_row[m[move]]==" ":
this_row.append[m[move]]=turn
display_instruct()
computer,human=pieces()
board=new_board()
display_board(board)
move= int(input("Move?"))
legal_moves()
print ("Human:", human, "\nComputer:", computer)
#连接4
#使用我自己的格式化
随机输入
#定义全局变量
X=“X”
O=“O”
空=“\u0”
TIE=“TIE”
行数=6
NUM_COLS=8
def display_instruction():
“”“显示游戏说明。”“”
印刷品(
"""
欢迎来到有史以来第二大智力挑战:Connect4。
这将是你的人脑和我的硅处理器之间的摊牌。
你将通过输入一列数字1-7来表明你的行动。你的行动
(如果该列尚未填充)将移动到最低可用位置。
做好准备,人类。愿施瓦茨与你同在!\n
"""
)
def询问是否(问题):
“问一个是或不是的问题。”
响应=无
当响应不在“y”、“n”中时:
回答=输入(问题)。下()
返回响应
def ask_编号(问题、低、高):
“”“询问范围内的数字。”“”
#在PythonSense中使用range,即请求
#一个介于1和7之间的号码,拨打ask_号码,低=1,高=8
低=1
高=NUM\u COLS
响应=无
响应不在范围内时(低、高):
回答=int(输入(问题))
返回响应
def片段():
“”“确定是播放机还是计算机先行。”“”
先走=问“是”或“否”(“你需要先走吗?(y/n):”)
如果go_first==“y”:
打印(“\n然后迈出第一步。您将需要它。”)
人类=X
计算机=O
其他:
打印(“\n你的勇敢将是你的毁灭……我将先走。”)
计算机=X
人=O
返回计算机,人
def新_板():
董事会=[]
对于范围内的x(数量):
board.append([“”]*NUM_行)
返回板
def显示板(板):
“”“在屏幕上显示游戏板。”“”
对于范围内的r(NUM_行):
打印行(右板)
打印(“\n”)
def打印行(板,数量):
“”“从当前板打印指定行”“”
此行=板[num]
打印(“\n\t |“、此行[num]、“|”、此行[num]、“|”、此行[num]、“|”、此行[num]、“|”、此行[num]、“|”、此行[num]、“|”、此行[num]、“|”)
打印(“\t”,“|--|--|--|--|--|--|--|--|--|”)
#这里一切正常!
def法律行动(董事会):
“”“创建玩家可以放下棋子的列号列表”“”
法律=[]
如果move
在脚本的底部,您可以调用:
move= int(input("Move?"))
legal_moves()
# ^ no arguments
这不提供必要的板
参数,因此会显示错误消息