Python 玩家未定义

Python 玩家未定义,python,python-3.x,Python,Python 3.x,我对播放器未定义的错误有一些问题,这是错误和代码,我在图片中添加了链接。我对python非常陌生,我不理解大多数错误,如果有人能帮助我,我会很高兴的 我已经为您的附加图片创建了一个最低限度的工作代码。大多数情况下,您的全局变量处理是不正确的。您可以在下面的代码中找到我的注释作为注释 代码: # Define global variable current_player = None class Player(object): def __init__(self, name):

我对播放器未定义的错误有一些问题,这是错误和代码,我在图片中添加了链接。我对python非常陌生,我不理解大多数错误,如果有人能帮助我,我会很高兴的


我已经为您的附加图片创建了一个最低限度的工作代码。大多数情况下,您的全局变量处理是不正确的。您可以在下面的代码中找到我的注释作为注释

代码:

# Define global variable
current_player = None


class Player(object):
    def __init__(self, name):
        self.name = name
        self.other = 100


def main():
    option = input("Option: ")
    if option == "1":
        start()


def start():
    # Use the global variable inside the function.
    global current_player
    user_name = input("Name: ")
    # Assign the Player instance to the global variable.
    current_player = Player(name=user_name)
    start1()


def start1():
    # Use the global variable inside the function.
    global current_player
    # Get the "name" attribute of the "Player" object.
    print("Hello my friend: {}".format(current_player.name))


main()
>>> python3 test.py 
Option: 1
Name: Bill
Hello my friend: Bill

>>> python3 test.py 
Option: 1
Name: Jill
Hello my friend: Jill
输出:

# Define global variable
current_player = None


class Player(object):
    def __init__(self, name):
        self.name = name
        self.other = 100


def main():
    option = input("Option: ")
    if option == "1":
        start()


def start():
    # Use the global variable inside the function.
    global current_player
    user_name = input("Name: ")
    # Assign the Player instance to the global variable.
    current_player = Player(name=user_name)
    start1()


def start1():
    # Use the global variable inside the function.
    global current_player
    # Get the "name" attribute of the "Player" object.
    print("Hello my friend: {}".format(current_player.name))


main()
>>> python3 test.py 
Option: 1
Name: Bill
Hello my friend: Bill

>>> python3 test.py 
Option: 1
Name: Jill
Hello my friend: Jill
注意:

# Define global variable
current_player = None


class Player(object):
    def __init__(self, name):
        self.name = name
        self.other = 100


def main():
    option = input("Option: ")
    if option == "1":
        start()


def start():
    # Use the global variable inside the function.
    global current_player
    user_name = input("Name: ")
    # Assign the Player instance to the global variable.
    current_player = Player(name=user_name)
    start1()


def start1():
    # Use the global variable inside the function.
    global current_player
    # Get the "name" attribute of the "Player" object.
    print("Hello my friend: {}".format(current_player.name))


main()
>>> python3 test.py 
Option: 1
Name: Bill
Hello my friend: Bill

>>> python3 test.py 
Option: 1
Name: Jill
Hello my friend: Jill
我建议去掉全局变量的用法。将所需变量作为参数传递。我已经实现了一个不包含全局变量的版本

def start():
    user_name = input("Name: ")
    # Assign the Player instance to the global variable.
    current_player = Player(name=user_name)
    start1(current_player)


def start1(current_player_info):
    # Get the "name" attribute of the "Player" object.
    print("Hello my friend: {}".format(current_player_info.name))
PS:

# Define global variable
current_player = None


class Player(object):
    def __init__(self, name):
        self.name = name
        self.other = 100


def main():
    option = input("Option: ")
    if option == "1":
        start()


def start():
    # Use the global variable inside the function.
    global current_player
    user_name = input("Name: ")
    # Assign the Player instance to the global variable.
    current_player = Player(name=user_name)
    start1()


def start1():
    # Use the global variable inside the function.
    global current_player
    # Get the "name" attribute of the "Player" object.
    print("Hello my friend: {}".format(current_player.name))


main()
>>> python3 test.py 
Option: 1
Name: Bill
Hello my friend: Bill

>>> python3 test.py 
Option: 1
Name: Jill
Hello my friend: Jill

在下一个问题中,不要附加或链接图片。请添加最小代码和您的问题(回溯)。请阅读:

请不要只提供代码链接。你为什么不把它作为格式化的文本包含在你的问题中?它只是放在一行中,我们不在乎事情是在一行中。更重要的是将代码作为文本包含,这样人们就可以在需要测试某些内容时复制/粘贴代码,而不是键入整个内容。这有助于我们帮助你。你的问题应该是自我-contained@Vilhemiuu欢迎来到SO!请参阅以了解什么构成了一个好问题。您应该将代码作为文本而不是图像发布。不管怎样,你的错误是由于打字错误。您将该类定义为
player
,并将其称为
player
。。。注意
p/p
谢谢我用的是这个家伙的代码