How to reuse user's raw input in other function
Question: I m making a 2D board game for python for hw.

I asked user to input a integer for the board size. for example, 7. I have
modified a bit (only show the important ones) before posting. the function is
like follows

    
    
    def asksize():           
        while True:
            ask=raw_input("Input board size: ")  
            try:
                size=int(ask)
                return size
    
            except ValueError: 
                print "Please enter a integer"   
    

Because it is variable board size, I need reuse the variable size in other
function, use it for checking user's move is valid or not, how can I reuse the
variable?

    
    
    def checkmove(move):    
        #move is sth like eg. A1:B2
        move=move.split(":") #I split it so it becomes ['A','1']['B','2']  
        if size>=int(move[0][1]) and int(move[0][1])>=1 and size>=int(move[1][1]) and int(move[1][1])>=1: #for example if board size is 7, this is to check whether user input is between 1 to 7 within the board
            return True
        else:
            return False 
    

In my checkmove function, I can't use size in my argument because it is not
defined, how can I make it workable?

Thanks


Answer: Making the variable size global is an option, but you should consider your
functions as an API, and use them on the game main bucle.

so storing the input like this:

    
    
    size = asksize() #store the data on to variable called size
    #then just call your check move
    checkmove(x)
    

Anyway this is a bad exercise, its better to pass the variable through the
function inside the game code:

    
    
    #definitions
    def asksize():
        #your code here
    def checkmove(move, size):
        #your code here
    
    
    #game set up here
    size = asksize()
    #more set up stuff
    #end of game set up
    #Game Main code
    
    while True: #main game bucle
    #Game stuff here
    checkmove(move, size)
    #more game stuff
    

