def结束时的变量重置

时间:2017-09-12 15:03:58

标签: python

我几天前才开始学习Python,但我决定通过制作一个简单的X O游戏来测试我的技能,下面是我到目前为止编写的代码。 希望它不需要解释。但基本上发生的事情是在其中一个功能结束后继续进行另一个数字重置,所以当玩家一输入" 1"并且X被放置在位置1然后玩家两个输入" 2"并且O被放置在位置2,输入的玩家1的X回到" 1"。 我该如何解决这个问题?

grid=" %s | %s | %s \n --+---+--\n %s | %s | %s\n --+---+--\n %s | %s | %s"  
one = "1" 
two = "2" 
three = "3" 
four = "4"
five = "5" 
six = "6" 
seven = "7" 
eight = "8" 
nine = "9" 
error = "\n error" 
print grid % (one, two, three, four, five, six, seven, eight, nine)

def pleone(m, one, two, three, four, five, six, seven, eight, nine): 
    if m == "1":
        one = "X"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        pltwo()   
    elif m == "2":
        two = "X"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        pltwo()   
    elif m == "3":
        three = "X"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        pltwo()   
    elif m == "4":
        four = "X"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        pltwo()  
    elif m == "5":
        five = "X"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        pltwo()   
    elif m == "6":
        six = "X"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        pltwo()   
    elif m == "7":
        seven = "X"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        pltwo()   
    elif m == "8":
        eight = "X"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        pltwo()   
    elif m == "9":
        nine = "X"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        pltwo()   
    else:
        print error

def pletwo(k, one, two, three, four, five, six, seven, eight, nine): 
    if k == "1":
        one = "O"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        plone()   
    elif k == "2":
        two = "O"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        plone()   
    elif k == "3":
        three = "O"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        plone()   
    elif k == "4":
        four = "O"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        plone()   
    elif k == "5":
        five = "O"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        plone()   
    elif k == "6":
        six = "O"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        plone()   
    elif k == "7":
        seven = "O"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        plone()   
    elif K == "8":
        eight = "O"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        plone()   
    elif k == "9":
        nine = "O"
        print grid % (one, two, three, four, five, six, seven, eight, nine)
        plone()   
    else:
        print error

def plone():   
    m = raw_input("\n player one (X), what position? ")  
    pleone(m, one, two, three, four, five, six, seven, eight, nine)

def pltwo():   
    k = raw_input("\n player two (O), what position? ")  
    pletwo(k, one, two, three, four, five, six, seven, eight, nine)

plone()

1 个答案:

答案 0 :(得分:2)

我按原样保留了你的代码,但你可以美化它。

更改回' 1'是那个'一个'在pleone和pletwo功能之外与' one'不同。在功能内部。

外面的'一个'具有全球范围,而“一个人”具有全球范围。函数内部具有局部范围,这意味着它们仅在函数内部有效。

您需要将函数内部的变量声明为全局变量才能更改它。

以下示例说明如何解决此问题:

routerLink="/forBusiness"