位置参数未定义

时间:2014-03-26 23:44:56

标签: debugging python-3.x

我正在开发一个更大的项目来编写代码,以便用户可以在计算机上玩Connect 4。现在,用户可以选择是否先行并绘制电路板。虽然为了确保用户只能输入合法动作而进行修正,但我遇到了一个问题,我的函数legal_moves()采用了1个位置参数,并给出了0,但我不明白我需要对男性做什么一切同意。

#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)

1 个答案:

答案 0 :(得分:2)

在脚本底部,您可以调用:

move= int(input("Move?"))
legal_moves()
          # ^ no arguments

这不提供必要的board参数,因此出现错误消息。