2011-12-18 132 views
0

所以我是一个Python的总noob所以我很抱歉,如果我没有足够具体。如何从函数中获取变量?

我写一个井字游戏程序和最困难的事情是找开关关

所以我写了一个函数来做到这一点,但我不能得到的“转”变量,它属于另一个功能。对不起,如果我是无知的,但这是最好的,我可以解释它。感谢您的时间:)

BTW这里是代码

x="X" 
o="O" 
EMPTY=" " 
TIE="TIE" 
NUM_SQUARES=9 

def display_instruct(): 
    print(
    """ 
    Welcome to the greatest challenge ever 

    Tic 
    Tac 
    Toe 

    the board is as shown 

    0|1|2 
    ------ 
    3|4|5 
    ----- 
    6|7|8 


    Prepare as teh game is about to start 
    """) 

def yesno(question): 
    response=None 
    while response not in ("y","n"): 
     response=input(question).lower() 
    return response 
def asknum(question, low, high): 
    response=None 
    while response not in range(low,high): 
     response=int(input(question)) 
    return response 

def pieces(): 
    go_first=yesno("Do you want the first move") 

    if go_first=="y": 
     print("then take the first move you will need it") 
     human=x 
     computer=o 
     turn=x 
     return computer, human 
    else: 
     print ("Ok i shall go first") 
     computer=x 
     human=o 
     turn=x 
     return computer, human 



def new_board(): 
    board=[] 
    for square in range(NUM_SQUARES): 
     board.append(EMPTY) 
    return board 

def display_board(board): 
    print ("\n") 
    print (board[0],"|",board[1],"|",board[2]) 
    print ("----------") 
    print (board[3],"|",board[4],"|",board[5]) 
    print ("----------") 
    print (board[6],"|",board[7],"|",board[8]) 

def legal_moves(board): 
    moves=[] 
    for square in range(NUM_SQUARES): 
     if board[square]==EMPTY: 
      moves.append(square) 
    return moves 

def winner(board): 
    WAYS_TO_WIN=((0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6)) 

    for row in WAYS_TO_WIN: 
     if board[row[0]]==board[row[1]]==board[row[2]]!=EMPTY: 
      winner=[row[0]] 
      return winner 
     if EMPTY not in board: 
      return TIE 
     return None 



def human_move(board,human): 
    legal=legal_moves(board) 
    move=None 
    print (legal) 
    while move not in legal: 
     move=asknum("Where will you move",0,NUM_SQUARES) 
    return move 

def computer_move(board,computer,human): 
    board=board[:] 
    BEST_MOVES=(4,0,2,6,8,1,3,5,7) 
    print ("I shall take square number", end=" ") 
    for move in legal_moves(board): 
     board[move]=computer 
     if winner(board)==computer: 
      print(move) 
      return move 
    for move in legal_moves(board): 
     board[move]=human 
     if winner(board)==human: 
      print(move) 
     return move 

    board[move]=EMPTY 

    for move in BEST_MOVES: 
     if move in legal_moves(board): 
      print (move) 
     return move 

def next_turn(turn): 
    if turn==x: 
     return o 
    else: 
     return x 
def congrat_winner(the_winner,computer,human): 
    if the_winner!=TIE: 
     print(the_winner, "WON") 
    else: 
     print ("Its a ttie") 
    if the_winner==computer: 
     print ("I WON HA HA IN YO FACE") 
    elif the_winner==human: 
     print ("NO IT CANNOT BE") 
    elif the_winner==TIE: 
     print ("you were lucky this time") 
def curturn(turn,computer,human): 

    if turn==x and move!="" and move!=" ": 
     turn=o 
    elif turn==o and move!="" and move!=" ": 
     turn=x 


def main(): 
    display_instruct() 

    computer, human=pieces() 
    turn=x 
    board=new_board() 
    display_board(board) 
    curturn(turn,computer,human) 
    while not winner (board): 
     display_board(board) 


     if turn == human: 
      move=human_move(board,human) 
      board[move]=human 
     else: 
      move=computer_move(board,computer,human) 
      board[move]=computer 
      display_board(board) 
      turn=next_turn(turn) 
      the_winner=winner(board) 
      congrat_winner(the_winner,computer,human) 




main() 
+0

你想要什么'turn'变量但不能得到它?请澄清你的实际问题,也许只显示代码有问题 – codeling 2011-12-18 03:15:38

+0

对不起,我希望它在功能curturn谢谢:) – ollien 2011-12-18 03:18:04

+0

对我来说,它不是很清楚你问 - 你已经通过转弯变量进入削波函数,所以有什么问题? – codeling 2011-12-18 03:20:17

回答

2

功能看起来像这样(简化):

def functionname(parameter1, parameter2=foo): 
    code that does stuff 
    return value 

如果你在你的函数想从调用访问变量函数,您将它作为参数传入。如果您从调用函数中访问您调用的函数中的变量,则返回它。

你也可以使用全局变量(你这样做,我知道),这是一个糟糕的想法,因为它会让你的程序混乱,很难调试。所以如果可以的话,尽量避免(尽管在这样的小程序中,这不是一场灾难,所以现在不要担心)。

如果你想从完全不同的地方访问变量,你可能想用类和东西来研究面向对象的编程。