在赋值之前引用变量'player1'

时间:2015-04-21 12:00:14

标签: python python-3.x

我尝试输入两个玩家名称并让程序识别名称中除了字母字符以外的任何内容。名称应循环,直到输入正确;目前,我收到错误local variable 'player1' referenced before assignment.

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()

我正在阅读全球作业,在这种情况下听起来很糟糕;还有其他建议吗?

3 个答案:

答案 0 :(得分:2)

在对其执行方法之前,您必须先定义player1。

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

它不是全局因为它在函数中

答案 1 :(得分: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)

答案 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

return循环中的while会在您欢迎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不会以alpha身份返回,因此您永远不会被提示输入。请参阅以下内容:

>>> 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()函数。或者您已准备好将相同的功能放入下一个游戏中。