在定义前引用的变量,用于生成零和交叉/Tic Tac Toe gam

2024-09-25 02:41:18 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我一直在做一个Xs和Os游戏,作为python3.4的一个附带项目,我遇到了一个问题。虽然它可以工作,但它不保存板,因为我必须在def game():中定义插槽

如果我在def game():之外定义插槽,即使我将它们全球化,也会出现错误,slot 1(等等)referenced before definition或者其他什么。 这是到目前为止我的代码,因为我想要一个循环,我必须把游戏放在一个def game():

from time import sleep


gameboard = """
[1]
   [2]
      [3]
[4]
   [5]
      [6]
[7]
   [8]
      [9]
"""
print ("Welcome to Noughts and Crosses")
print ("The gameboard looks like this")
print (gameboard)


turn = (0)

def globalvar():
    global slot1
    global slot2
    global slot3
    global slot4
    global slot5
    global slot6
    global slot7
    global slot8
    global slot9
    global slotkeeper
    global slotkeeper1

globalvar()


global slotkeeper1
slotkeeper = (1)

def game():
    print ("Turn" ,turn + 1)
    xturn = input("X, please type a number where you want to place your marker")
    if xturn == ("1"):
        slot1 = """
[X]
    """
    elif xturn == ("2"):
        slot2 = """
    [X]
    """
    elif xturn == ("3"):
        slot3 = """
        [X]
    """
    elif xturn == ("4"):
        slot4 = """
[X]
"""
    elif xturn == ("5"):
        slot5 = """
    [X]
    """
    elif xturn == ("6"):
        slot6 = """
        [X]
    """
    elif xturn == ("7"):
        slot7 = """
[X]
    """
    elif xturn == ("8"):
        slot8 = """
    [X]
    """
    elif xturn == ("9"):
        slot9 = """
        [X]
    """
    oturn = input("O, please type a number where you want to place your marker")
    if oturn == ("1"):
        slot1 = """
[O]
    """
    elif oturn == ("2"):
        slot2 = """
    [O]
    """
    elif oturn == ("3"):
        slot3 = """
        [O]
    """
    elif oturn == ("4"):
        slot4 = """
[O]
    """
    elif oturn == ("5"):
        slot5 = """
    [O]
    """
    elif oturn == ("6"):
        slot6 = """
        [O]
    """
    elif oturn == ("7"):
        slot7 = """
[O]
    """
    elif oturn == ("8"):
        slot8 = """
    [O]
    """
    elif oturn == ("9"):
        slot9 = """
        [O]
    """

    while slotkeeper == (1):
        slot1 = """
[1]
"""
        slot2 = """
    [2]
"""
        slot3 = """
         [3]
"""
        slot4 = """
[4]
"""
        slot5 = """
    [5]
"""
        slot6 = """
         [6]
"""
        slot7 = """
[7]
"""
        slot8 = """
    [8]
"""
        slot9 = """
         [9]
"""
        slotkeeper = (0)

    sleep (0.6)    
    print (slot1, slot2, slot3)
    print (slot4, slot5, slot6)
    print (slot7, slot8, slot9)



def game2():
    sleep (0.6)
    slotkeeper = (0)
    game()

def gamefinal():
    game()
    game2()


gamefinal() 

如果您运行它,您将看到我的问题是什么,因为我需要在game()的主定义(即while cond1 == (1))内生成一个while循环 所以基本上,当一个条件为真时,插槽保持这种方式,否则它不会在两个回合之间保存电路板,并且我不能在主def game():之外定义cond1,或者,即使在定义之前没有引用错误的插槽,即使我将它们全球化。你知道吗

如果我将插槽的主定义放在def game():之外

       slot1 = """
[1]
"""
        slot2 = """
    [2]
"""
        slot3 = """
         [3]
"""
        slot4 = """
[4]
"""
        slot5 = """
    [5]
"""
        slot6 = """
         [6]
"""
        slot7 = """
[7]
"""
        slot8 = """
    [8]
"""
        slot9 = """
         [9]
"""

当它打印插槽时,为了显示gameboard的当前状态,它会转到定义之前引用的错误变量,就像我说的,即使我全球化了它们,即使没有在def globalvar():中全球化它们,正常情况下,我仍然会遇到这个问题!如果我在def game():的开始处定义插槽,它不会保存电路板(因为它将插槽重新定义为那些,显然),如果我在它的开始处放置while循环,它只会停止,并保持原样,在print (gameboard)之后,它什么也不做,显然程序仍在运行,因为它是,因为while循环需要为true,或者在它后面有一个else语句,它不起作用,只是打断了它!你知道吗

编辑:我试着用引号括住slotkeeper = ("1"),放在括号里,把while slotkeeper == (1):改成while slotkeeper == ("1"),仍然没有改变。D:是的 编辑编辑:它确实起到了作用,但是没有得到错误消息,它只是卡住了,即使我已经将变量slotkeeper全球化并将其设置为("1"),然后在def game2():中将其设置为("0")!你知道吗


Tags: game定义defglobal插槽printelifwhile
2条回答

您严重误解了Python的^{}语句的工作原理。不能用它来声明全局变量。无需声明任何内容,在模块顶层进行的任何赋值都会创建一个全局变量。你知道吗

相反,您可以在一个函数中使用它,在这个函数中您希望能够修改在函数外部定义的变量。因此,将当前在globalvar函数中的global语句(它们不做任何有用的事情)移到game函数中。这将允许game中的代码修改全局变量(并避免出现异常)。你知道吗

请注意,使用全局变量通常是设计不佳的征兆。尤其是当你有很多的时候,就像你现在在你的程序里做的那样。作为更好设计的开始,我强烈建议使用列表或其他数据结构来保存董事会数据,而不是使用一堆单独的变量。你知道吗

我们使用global关键字来访问全局变量,而不是声明它。Python是一种动态语言,无论如何,变量声明没有多大意义。你知道吗

其次,为什么你需要这么多全局变量呢。我相信你能想出其他优雅的方法。你知道吗

第三,请不要使用那么多if/elif语句,而是使用dict语句,这样会更加python-

slot_options = {
     "1": """
     [X]
     """,
     "2": """
         [X]
     """
     ...
}

slots = {}
slots[xturn] = slot_options[xturn]

这条线上有些东西。你知道吗

第四,这些代表“X”位置的“图纸”是怎么回事。你知道吗

我建议您先检查一些当前实现的代码,然后自己设计。它可能会给你一些提示。你可以看看这个-Sample tic tac toe gist

相关问题 更多 >