2015-04-21 14 views
0

我試圖輸入兩個玩家的名字並讓程序識別名稱中除字母字符以外的任何內容。名稱應該循環,直到它們輸入正確;目前,我得到local variable 'player1' referenced before assignment.變量'player1'在作業之前引用了

def player(): 

    while (player1.isalpha()): 
     player1 = input("What is the name of our first player? \n") 
     print("Welcome " + player1) 
     return 
    else: 
     print("Please enter a name without integers or spaces") 
     return False 


    while (player2.isalpha()): 
     player2 = input("What is the name of our first player? \n") 
     print("Welcome " + player2) 
     break 
    else: 
     print("Please enter a name without integers or spaces") 
     return True 
player() 

我讀關於全球分配和聽起來在這種情況下,不良的錯誤;任何其他建議?

+0

您正在**'player1 = input(...)'之前測試'player1.isalpha()'** ... – jonrsharpe

+0

[詢問用戶輸入信息直到他們給出有效響應]( http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – jonrsharpe

+0

那麼,'player1'沒有被定義或分配,然後再調用' isalpha()'方法,導致這個錯誤。順便說一句,你的代碼不會做你認爲它應該做的事。 – MasterAM

回答

2

在對其執行方法之前,您必須定義player1。

player1="" 
player2="" 
while... 

它不是全球的,因爲它的功能

1
def player(x): 
    player='' 
    pass=0 
    while (not player.isalpha()): 
     if pass != 0: 
      print("Please enter a name without integers or spaces") 
     pass += 1 
     player = input("What is the name of our player No.{}?\n".format(x)) 
    print("Welcome {} No.{}".format(player,x)) 
    return player 

player1=player(1) 
player2=player(2) 
1

你,因爲你使用player1player2定義之前收到此錯誤。這很容易解決:

def player(): 
    player1 = '' 
    player2 = '' 

    # The rest of your code here... 

雖然你的代碼還有一些其他問題。例如:

while (player1.isalpha()): 
    player1 = input("What is the name of our first player? \n") 
    print("Welcome " + player1) 
    return 
else: 
    print("Please enter a name without integers or spaces") 
    return False 

returnwhile循環將作爲你的歡迎player1儘快退出功能。但是,你說你想提示第二個玩家,所以它應該是break。接下來,當我做這些更正,然後刪除該函數入解釋,這是我得到:

>>> def player(): 
...  player1 = '' 
...  player2 = '' 
...  while (player1.isalpha()): 
...   player1 = input("What is the name of our first player? \n") 
...   print("Welcome " + player1) 
...   break 
...  else: 
...   print("Please enter a name without integers or spaces") 
...   return False 
...  while (player2.isalpha()): 
...   player2 = input("What is the name of our first player? \n") 
...   print("Welcome " + player2) 
...   break 
...  else: 
...   print("Please enter a name without integers or spaces") 
...   return True 
... 
>>> player() 
Please enter a name without integers or spaces 
False 
>>> 

你空player1 & player2都不會回來了α,所以你永遠不會得到提示用於輸入。請參閱以下內容:

>>> player1 = '' 
>>> assert player1.isalpha() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AssertionError 
>>> 

更新

通過我以爲這下一塊的時候,別人也發佈了類似的回答。我將包括我的看看在做不同的方式。

我真的使用類似以下內容:

>>> def get_player(n=1): 
... player = input("What is the name of player %d? " % n) 
... if not player.isalpha(): # Ask again, if non-alpha characters. 
...  print("Please enter a name without integers or spaces!") 
...  player = get_player(n) 
... return player 
... 
>>> player1 = get_player(1) 
What is the name of player 1? Bob1 
Please enter a name without integers or spaces! 
What is the name of player 1? Bob 
>>> assert player1 == 'Bob' 
>>> 

這將允許你要求的球員,比如任意數:

>>> players = [] 
>>> try: 
... num = int(input("How many players? ")) 
... except ValueError: 
... print("Number of players must be an integer!") 
... else: 
... for i in range(num): 
...  players.append(get_player(i + 1)) 
... 
How many players? 3 
What is the name of player 1? Frodo 
What is the name of player 2? Sam 
What is the name of player 3? Merry 
>>> assert players == ['Frodo', 'Sam', 'Merry',] 
>>> 

這樣,如果你目前的遊戲tic-tac-toe(或其他)有朝一日會成爲一個有n個玩家的n維遊戲場的超級井字遊戲,你不必完全重寫和重新調試你的player()函數。或者您已準備好將相同的功能放入您的下一個遊戲中。

相關問題