d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Python - Getting Information From Other Functions > Help Please
Add Reply New Topic New Poll
Member
Posts: 8,407
Joined: Jan 29 2008
Gold: 450.00
May 28 2012 05:18pm
Hi guys,

Really confused how can I pass out information from functions then return it out so other functions can use it?



def error_move(board):
'''User Move'''
.......
Specifically here...


x_cod = board[int(row)] assigning
y_cod = [int(field)] assigning
return(x_coord, y_coord) #pass out

def player_move(x_coord, y_coord): #pass in ERROR - NameError: global name 'x_coord' is not defined
(x, y) = x_coord, y_coord
print x, y




How do I make it so it works im so confused and very fustrated :(

PLEASE HELP ANYONE



Code
# To start type: play(None)


PROMPT_SIZE = "Board size: "
ERROR_SIZE = "Size must be a number between 4 and 9"
PROMPT_MOVE = "Player X, enter move: "  # Or player O ...
ERROR_MOVE_LENGTH = "Move must be 2 characters"
ERROR_MOVE_POSITION = "Invalid position" # you can't place your player on occupied position
ERROR_MOVE_FORMAT = "Invalid input format" # if you play, e.g., A1 instead of 1A
ERROR_MOVE_ILLEGAL = "Illegal move" #if move does not result in linearly enclosed opponents
WIN = "Win for player X!"   # Or player O
DRAW ="Equal number of frendz. Game is drawn!"


def get_board_size():
   '''Get board size'''
   PPT_SIZE = raw_input(PROMPT_SIZE) #Asking for board size
   while PPT_SIZE.isdigit() == False or int(PPT_SIZE) > 9 or int(PPT_SIZE) < 4:
       print ERROR_SIZE # size error not between 4 & 9
       PPT_SIZE = raw_input(PROMPT_SIZE) #repeating until correct input
   return PPT_SIZE      
   
   
def make_board(size):
   ''' Creating the board'''
   board = []
   board_odd_or_even = size % 2
   for i in range(0, size):
       list = []
       for i in range(0,size):
           list.append('.') # dots for empty cells
       board.append(list)    
   first_point = (size / 2) - 1
   board[first_point][first_point] = 'X' # Player X starting pieces
   board[first_point][first_point + 1] = 'O' # player O starting pieces
   board[first_point + 1][first_point] = 'O' # player O starting pieces
   board[first_point+ 1][first_point + 1] = 'X' # Player X starting pieces
   return board


def display_board(board):
   '''Displaying the board'''
   print # needs a space after board prompt
   i = len(board)
   num = 0
   alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'] # reference
   for line in board:
       num = num + 1
       print num, ' '.join(line) # printing board
   letter_count = 0
   print ' ',
   for letter in alpha:
       letter_count += 1
       if letter_count <= i:  
           print letter, #printing alpha reference    
   print # fixes bug
   print # needs a line after board
 
   
def error_move(board):
   '''User Move'''
   alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
   
   user_move=raw_input(PROMPT_MOVE)
   
   is_done1 = False
   is_done2 = False
   
   while len(user_move) != 2:
       print ERROR_MOVE_LENGTH # Input must be 2 chars
       user_move=raw_input(PROMPT_MOVE)
       
   while user_move[0].isalpha() == False or user_move[1].isdigit() == False:
       print ERROR_MOVE_FORMAT # format must be letter then number eg. A1
       user_move=raw_input(PROMPT_MOVE)
       
   while int(user_move[1]) > len(board) or int(user_move[1]) == 0:
       print ERROR_MOVE_ILLEGAL # Number Move number must be on board
       user_move=raw_input(PROMPT_MOVE)
       
   while is_done1 == False:
       if alpha.__contains__(user_move[0].upper()): #If letter is a letter on board
           letter_number = alpha.index(user_move[0].upper()) + 1
           if letter_number > len(board):
               print ERROR_MOVE_POSITION # Letter is in Alpha but not right size
               user_move=raw_input(PROMPT_MOVE)
               is_done1 = False
           else:
               is_done1 = True #Letter is in Aplha and is right size
       else:
           print ERROR_MOVE_POSITION #letter not in Alpha and is not on board.
           user_move=raw_input(PROMPT_MOVE)
           is_done1 = False
           
   while is_done2 == False:
       field = alpha.index(user_move[0].upper())
       row = int(user_move[1]) - 1
       if board[int(row)][int(field)].__contains__('.') == False:
           print ERROR_MOVE_POSITION # Peice already in place
           user_move=raw_input(PROMPT_MOVE)
           is_done2 = False
       else:
           is_done2 = True
   x_cod = board[int(row)]
   y_cod = [int(field)]
   return(x_coord, y_coord) #pass out

def player_move(x_coord, y_coord): #pass in
   (x, y) = x_coord, y_coord
   print x, y
   
def play(initial_board_config): # main game function
   '''Main Play function'''
   if initial_board_config is None:
       board_size = get_board_size() # 1. Prompt user for board size
       board = make_board(int(board_size))
   else:
       board_size = len(initial_board_config)
       board = initial_board_config
       
   display_board(board) # 3. Display the board
   error_move(board) #4. Check if players move is allowed
   player_move(error_move(board))
           
# if __name__ == '__main__':
# play(None)
Member
Posts: 8,407
Joined: Jan 29 2008
Gold: 450.00
May 28 2012 05:31pm
may hav something to do with the input 'player_move(error_move(board))'
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 28 2012 05:32pm
Quote (BadKiwi @ May 28 2012 07:18pm)
Hi guys,

    x_cod = board[int(row)]  assigning
    y_cod = [int(field)]  assigning
    return(x_coord, y_coord) #pass out

def player_move(x_coord, y_coord): #pass in ERROR - NameError: global name 'x_coord' is not defined
    (x, y) = x_coord, y_coord
    print x, y



looks like a typo? you spelled it two different ways
Member
Posts: 8,407
Joined: Jan 29 2008
Gold: 450.00
May 28 2012 05:36pm
Quote (carteblanche @ May 28 2012 11:32pm)
looks like a typo? you spelled it two different ways


Oh wow thanks, thats half the mistake hahah :P

Now I get this error :

TypeError: player_move() takes exactly 2 arguments (1 given)

def play(initial_board_config): # main game function
......

display_board(board) # 3. Display the board
error_move(board) #4. Check if players move is allowed
player_move(error_move(board)) what should this be?

or def player_move(x_coord, y_coord): be?

Thanks so much for helping me :)
Member
Posts: 8,407
Joined: Jan 29 2008
Gold: 450.00
May 28 2012 06:07pm
@carteblanche do you kw mun?

apparntly im returning both the x and y coords, it's returning a tuple. And you're telling player_move that it needs two integers.

How do i fix that

This post was edited by BadKiwi on May 28 2012 06:21pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 28 2012 06:54pm
it's been a while, but i think you can just call [0] and [1] on it like an array to get your two parameters. just use a local variable first so you dont have to call the function twice

and i have no idea wtf kw mun means.
Member
Posts: 13,630
Joined: Dec 4 2009
Gold: 0.00
May 28 2012 10:59pm
It's returning a tuple because you have the parenthesis. Get rid of them, and it should return them both. Or you can do as carte said, and use the index to get to your values. You'll need to change the parameters and everything else in that function to accommodate the changed args, though.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll