2012-10-22 16 views
0

我目前遇到以下錯誤:爲什麼我會得到一個全球性的名稱錯誤

NameError: global name 'player1_unam' is not defined 

如果有人能請瀏覽下面的代碼,並給我一個soultion的問題,將不勝感激。

因爲我知道這是非常混亂和低效的代碼,但我只是python的初學者,所以我提前做了alpologise。

import os 
import random 
from random import randint 
game_won = False 
p1_playing = True 
p2_playing = True 
class start_game(object): 
    def __init__(self): 
     suspect = ["Miss Scarlet","Colonel Mustard","Mrs. White","Reverend Green","Mrs. Peacock","Professor Plum"] 
     weapon = ["Candlestick","Dagger","Lead Pipe","Revolver","Rope","Spanner"] 
     room = ["Kitchen","Ballroom","Conservatory","Billiard Room","Library","Study","Hall","Lounge","Dining Room","Cellar"] 
     rndsuspect = random.randrange(0, 5 ,1) 
     rndweapon = random.randrange(0,5,1) 
     rndroom = random.randrange(0,9,1) 
     answers = [] 
     answers.append(suspect[rndsuspect]) 
     answers.append(weapon[rndweapon]) 
     answers.append(room [rndroom])  
     print answers[0] 
     print answers[1] 
     print answers[2] 

class game_type: 
    def __init__(self): 
     select_game = raw_input("Do you want to start the game? ") 
     lstchar = ["Miss Scarlet","Colonel Mustard","Mrs. White","Reverend Green","Mrs. Peacock","Professor Plum"] 
     if select_game.lower() == "yes": 
      player1_unam = raw_input("What is Player 1's Name? ").lower().title() 
      player1_cindex = int(input('Choose A Character: Miss Scarlet[1], Colonel Mustard [2], Mrs. White [3], Reverand Green [4], Mrs. Peacock [5], Professor Plum [6] ')) 
      player1_char = lstchar[player1_cindex - 1] 
      print ("%s has chosen to play as %s") %(player1_unam,player1_char) 
      player2_unam = raw_input("What is Player 2's Name? ").lower().title() 
      if player2_unam == player1_unam: 
       while player2_unam == self.player1_unam: 
        player2_unam = player2_unam = raw_input("You can't use the same Name as Player 1, please choose again: ") 
      player2_cindex = input('Choose A Character: Miss Scarlet[1], Colonel Mustard [2], Mrs. White [3], Reverand Green [4], Mrs. Peacock [5], Professor Plum [6] ') 
      player2_char = lstchar[player2_cindex - 1] 
      if player2_cindex == player1_cindex: 
       while player2_cindex == player1_cindex: 
        print "Choose A Character Other Than: %s" % lstchar[player1_cindex - 1] 
        player2_cindex = int(input('Choose A Character: Miss Scarlet[1], Colonel Mustard [2], Mrs. White [3], Reverand Green [4], Mrs. Peacock [5], Professor Plum [6] ')) 
        player2_char = lstchar[player2_cindex - 1] 
        print ("%s has chosen to play as %s") %(player2_unam,player2_char) 

class dice_roll(game_type): 
    def __init__(self, game_type): 
     final_suggestion = False 
     suggested_suspect = ["Miss Scarlet","Colonel Mustard","Mrs. White","Reverend Green","Mrs. Peacock","Professor Plum"] 
     suggested_weapon = ["Candlestick","Dagger","Lead Pipe","Revolver","Rope","Spanner"] 
     rolled_room = ["Kitchen","Ballroom","Conservatory","Billiard Room","Library","Study","Hall","Lounge","Dining Room","Cellar", "Corridor"] 
     current_player = [player1_unam,player2_unam] 
     chosen_room = [] 
     player1_room = [] 
     player2_room = [] 
     suggestion = [] 
     correct_elements = [] 
     introom = random.randrange(0,10,1) 
     super(dice_roll,self). __init__(self) 
     current_turn = 0 
     while game_won == False and p1_playing == True and p2_playing == True: 
      if raw_input("Type 'Roll' to start your turn %s" %current_player[current_turn]).lower() == "roll": 
       dice = random.randrange(1,6,1) 
       pass 
       print "You have rolled a %d" % dice 
       if dice == 1: 
        print "%s is now in the Corridor" % current_player[current_turn] 
        if current_turn == 0: 
         player1_room = rolled_room[introom] 
        else: 
         player2_room = rolled_room[introom] 
       elif dice == 2: 
        introom = random.randrange(0,10,1) 
        print "You are now in the %s" % rolled_room[introom]     
        if current_turn == 0: 
         player1_room = rolled_room[introom] 
        else: 
         player2_room = rolled_room[introom]     
       elif dice == 3: 
        print "%s is now in the Corridor" % current_player[current_turn] 
        if current_turn == 0: 
         player1_room = rolled_room[10] 
        else: 
         player2_room = rolled_room[10] 
       elif dice == 4: 
        introom = random.randrange(0,10,1) 
        print "You can move to the %s [%d] " % (rolled_room[introom], introom + 1) 
        introom = random.randrange(0,10,1) 
        print "You can move to the %s [%d]" % (rolled_room[introom], introom + 1) 
        if current_turn == 0: 
         introom = int(input("Which Room Do You Choose? ")) 
         player1_room = rolled_room[introom] 
        else: 
         player2_room = int(input("Which Room Do You Choose? ")) 
         player2_room = rolled_room[introom] 
       elif dice == 5: 
        print "%s is now in the Corridor" % current_player[current_turn] 
        if current_turn == 0: 
         player1_room = rolled_room[10] 
        else: 
         player2_room = rolled_room[10] 
       elif dice == 6: 
        introom = random.randrange(0,10,1) 
        print "You can move to the %s [%d] " % (rolled_room[introom], introom + 1) 
        introom = random.randrange(0,10,1) 
        print "You can move to the %s [%d]" % (rolled_room[introom], introom + 1) 
        introom = random.randrange(0,10,1) 
        print "You can move to the %s [%d]" % (rolled_room[introom], introom + 1) 
        if current_turn == 0: 
         player1_room = int(input("Which Room Do You Choose? ")) 
         player1_room = rolled_room[introom] 
        else: 
         player2_room = int(input("Which Room Do You Choose? ")) 
         player2_room = rolled_room[introom]     
       if player1_room == "Cellar" or player2_room == "Cellar": 
         if raw_input("Do you want to make a final suggestion? ").lower == "yes": 
          final_suggestion = True 
       elif player1_room != "Corridor" and player1_room != "Cellar" or player2_room != "Corridor" and player2_room != "Cellar": 
        suggestion_index = int(input("Who do you think committed the crime? Miss Scarlet[1], Colonel Mustard [2], Mrs. White [3], Reverand Green [4], Mrs. Peacock [5], Professor Plum [6]")) 
        suggestion.append(suggested_suspect[suggestion_index - 1]) 
        suggestion_index = int(input("What do you think was used to commit the crime? Candlestick [1], Dagger [2], Lead Pipe[3], Revolver[4], Rope[5], Spanner[6] ")) 
        suggestion.append(suggested_weapon[suggestion_index - 1]) 
        if current_turn == 0: 
         suggestion.append(player1_room) 
        else: 
         suggestion.append(player2_room) 
         if suggestion [0] == start_game.answers [0]: 
          correct_elements.append(suggestion[0]) 
         elif suggestion [1] == start_game.answers [1]: 
          correct_elements.append(suggestion[1]) 
         elif suggestion [2] == start_game.answers [2]: 
          correct_elements.append(suggestion[2]) 
         intcorrect = len(correct_elements)    
        if final_suggestion == True: 
          suggestion_index = int(input("Who do you think committed the crime? Miss Scarlet[1], Colonel Mustard [2], Mrs. White [3], Reverand Green [4], Mrs. Peacock [5], Professor Plum [6]")) 
          suggestion.append(suggested_suspect[suggestion_index - 1]) 
          suggestion_index = int(input("What do you think was used to commit the crime? Candlestick [1], Dagger [2], Lead Pipe[3], Revolver[4], Rope[5], Spanner[6] ")) 
          suggestion.append(suggested_weapon[suggestion_index - 1]) 
          suggestion_index = int(input("Where do you think the crime was committed? Kitchen [1],Ballroom [2],Conservatory [3], Billiard Room [4], Library [5], Study [6], Hall [7], Lounge [8], Dining Room [9], Cellar [10]")) 
          suggestion.append(rolled_room[suggestion_index - 1]) 
          if suggestion[0] == start_game.answers[0] and suggestion [1] == start_game.answers [1] and suggestion [2] == start_game.answers [2]: 
           game_won = True 
           print "%s Has won the game :D" %current_player[current_turn] 
           print "%s committed the murder with the %s in the %s" %(start_game.answers[0],start_game.answers[1],start_game.answers[2]) 
           print "Thankyou for playing CLUEDO" 
           if raw_input("Would you like to play again? ").lower == "yes": 
            run_game = game_type(__init__)   
       elif player1_room != "Corridor" and player1_room != "Cellar" or player2_room != "Corridor" and player2_room != "Cellar": 
         suggestion_index = int(input("Who do you think committed the crime? Miss Scarlet[1], Colonel Mustard [2], Mrs. White [3], Reverand Green [4], Mrs. Peacock [5], Professor Plum [6]")) 
         suggestion.append(suggested_suspect[suggestion_index - 1]) 
         suggestion_index = int(input("What do you think was used to commit the crime? Candlestick [1], Dagger [2], Lead Pipe[3], Revolver[4], Rope[5], Spanner[6] ")) 
         suggestion.append(suggested_weapon[suggestion_index - 1]) 
         if current_turn == 0: 
          suggestion.append(player1_room) 
         else: 
          suggestion.append(player2_room) 
         if suggestion [0] == start_game.answers [0]: 
          correct_elements.append(suggestion[0]) 
         elif suggestion [1] == start_game.answers [1]: 
          correct_elements.append(suggestion[1]) 
         elif suggestion [2] == start_game.answers [2]: 
          correct_elements.append(suggestion[2]) 
         intcorrect = len(correct_elements) 
       if intcorrect == 0: 
        print "None of those facts are correct" 
        del correct_elements[0:len(correct_elements)] 
        del suggestion[0:len(suggestion)] 
        else: 
         shown_card = random.choice(correct_elements) 
         print "You got %s correct" %shown_card 
         del correct_elements[0:len(correct_elements)] 
         del suggestion[0:len(suggestion)] 
      current_turn += 1     
      if current_turn == 2: 
       current_turn = 0 

run_game = game_type() 
suspectinfo = start_game()   
roll = dice_roll(game_type) 

run_game()#.__init__() 
suspectinfo()#.__init__() 
roll() 

謝謝你們的幫助

+1

類初始化器不是放置大量不相關代碼(如用戶輸入)的最佳位置。如果答案是「是」,則只定義'player1_unam'。在game_type初始值設定項中,但超出了範圍。其他班級不會有。 – Keith

回答

0

您需要使用的

player1_unam 

否則,當您進行初始分配,創建一個本地變量,而不是

self.player1_unam 

,當你試圖解引用(訪問)它時,Python首先尋找一個局部變量,然後尋找一個全局變量e,並且兩次都失敗了,它給了你那個錯誤。

+0

我仍然被顯示相同的錯誤:( – minimatt

+0

看到另一個答案 - 我選擇了一個例子,但你必須修復你的Python方法中的每個引用 –

0

您需要使用self.player1_unam,因爲python正在尋找global命名空間,而不是本地(在定義它的類的內部)命名空間。

例如,以下可以是在全局命名空間的值,而self.player1_unam在類定義:

def player1_unam(): 
    print "I am in the global namespace!" 

或變量:

player1_unam="I am in the global namespace!" 

或類:

class player1_unam(object): 
    pass 

值得注意的是,您將不得不更改每個使用player1_unamself.player1_unam爲了解決這個問題。

+0

他也在* another *類中使用該名稱,在這裏真的有幫助 – Keith

+0

有什麼辦法可以使用在一個方法中創建的變量嗎? – minimatt

+0

@minimatt在使局部變量成爲一個全局變量? –

0

爲什麼不從一開始就讓玩家變成一個變數? 我覺得那會更好。

class game_type: 
    def __init__(self): 
     global player1_unam 
     ... 
     player1_unam = raw_input("What is Player 1's Name? ").lower().title() 
相關問題