2016-07-06 65 views
0

我是新來的編碼,特別是在Python中。我大約在1天前開始使用codeacademy學習python,並且我的進度很快。在戰艦部隊結束後,它挑戰我通過看我能做什麼來挑戰自己。所以,我着手讓遊戲成爲雙人遊戲。但是,在完成程序後,它告訴我第二位玩家船上的列變量沒有被定義。爲什麼?變量未定義:但我認爲它是

下面是代碼:

board1 = [] 
board2 = [] 
for i in range(5): 
    board1.append(["O"] * 5) 
    board2.append(["O"] * 5) 
# Creates 2 boards, one for each player to view 

def printboard(board): 
    for row in board: 
     print " ".join(row) 
# Prints one of the boards, depending on which player is meant to see it 

def boardset1(): 
    print "Player 1, set your coordinates!" 
    ship_col1 = int(raw_input("X:")) 
    ship_row1 = int(raw_input("Y:")) 
    if ship_col1 not in range(1,6) or ship_row1 not in range(1,6): 
     print "Invalid coordinates!" 
     boardset1() 
    else: 
     ship_col1 = abs(ship_col1 - 5) 
     ship_row1 = abs(ship_row1 - 5) 
     for i in range(10): 
      print "" 
     print "Coordinates set!" 

def boardset2(): 
    print "Player 2, set your coordinates!" 
    ship_col2 = int(raw_input("X:")) 
    ship_row2 = int(raw_input("Y:")) 
    if ship_col2 not in range(1,6) or ship_row2 not in range(1,6): #< Issue is here, I think 
     print "Invalid coordinates!" 
     boardset2() 
    else: 
     ship_col2 = abs(ship_col2 - 5) #< Might be here 
     ship_row2 = abs(ship_row2 - 5) 
     for i in range(10): 
      print "" 
     print "Coordinates set!" 
# 2 above functions set coordinates based on player input 

def play1(): 
    printboard(board1) 
    print "Player 1: Where is the opponent's ship?" 
    guess_col1 = int(raw_input("X:")) 
    guess_row1 = int(raw_input("X:")) 
    if guess_col1 not in range(1,6) or guess_row1 not in range(1,6): 
     print "Invalid coordinates!" 
     play1() 
    else: 
     guess_col1 = abs(guess_col1 - 5) 
     guess_row1 = abs(guess_row1 - 5) 
     if board1[guess_col1][guess_row1] == "X": 
      print "You already guessed here!" 
      play1() 
     elif guess_col1 == ship_col2 and guess_row1 == ship_row2: 
      win = True 
      print "You have won!" 
     else: 
      board1[guess_col1][guess_row1] = "X" 
     print "You have missed!" 

def play2(): 
    if win == False: 
     printboard(board2) 
     print "Player 2: Where is the opponent's ship?" 
     guess_col2 = int(raw_input("X:")) 
     guess_row2 = int(raw_input("X:")) 
     if guess_col2 not in range(1,6) or guess_row2 not in range(1,6): 
      print "Invalid coordinates!" 
      play2() 
     else: 
      guess_col2 = abs(guess_col2 - 5) 
      guess_row2 = abs(guess_row2 - 5) 
      if board2[guess_col2][guess_row2] == "X": 
       print "You already guessed here!" 
       play2() 
      elif guess_col2 == ship_col1 and guess_row2 == ship_row1: 
       win = True 
       print "You have won!" 
      else: 
       board2[guess_col2][guess_row2] = "X" 
      print "You have missed!" 
# Play functions are for gameplay 
win = False 
boardset1() 
boardset2() 

for i in range(25): 
    if win == False: 
     play1() 
     play2() 
    else: 
     break 

立即播放機1猜測之後,出現此錯誤:

Traceback (most recent call last): 
     File "python", line 97, in <module> 
     File "python", line 59, in play1 
    NameError: global name 'ship_col2' is not defined 

任何意見或解決方案是值得歡迎的。越詳細越好,因爲我還在學習。

謝謝!

+0

'ship_col2'是定義*函數'boardset2'裏面*。因此它不是全局的,並且在'play1'函數中絕對不可見。 – Evert

+1

'boardset1()'需要全局ship_col1,ship_col2'在開始處添加。它定義了只在該函數內部可用的局部變量。讓它們成爲全局意味着其他功能可以使用它們。 – zondo

+1

建議:避免使用全局變量,並將變量作爲函數參數傳遞給函數,並在函數結尾(例如,在元組內)將其返回到調用該函數的代碼。 – Evert

回答

0

您的問題是變量ship_col2是在一個函數中定義的。這意味着它只存在於該功能運行完畢之後才被刪除。爲了使它在該函數和其他函數外可用,您必須將其聲明爲全局變量,您可以通過將其定義爲文件頂部的board1board2下的默認值來進行定義。你必須定義你想在多個函數中使用的所有變量。

下面是一些進一步的閱讀,可以幫助你更好的理解:http://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html

相關問題