用户在python中更改变量后如何继续更新变量?

时间:2014-05-11 05:03:05

标签: python

我正在制作一个游戏,用户输入“x”数量的筹码放入第1堆和第2堆。 (例如,用户输入:12;桩1:12,桩2:12)然后从第1堆或第2堆中取出“z”量的碎片,然后计算机从对面取出相同数量的碎片用户从中获取的桩。所以基本上计算机一直都在赢。但是我在更新桩时遇到一些麻烦,直到两桩达到0。

def initGame():
    pile1= chips
    pile2= chips
    finished = False
    while chips<=0:
        if chips <= 0:
            print("Please input a number greater than 0")
        finished= True
    else:
        finished= True
    return pile1, pile2

def displayPiles(pile1, pile2):
    print("It is your turn human.")
    print("Here are the piles: ")
    print("pile 1: "+ str(pile1))
    print("pile 2: "+ str(pile2))

    return pile1, pile2

def getHumanMove(x,y):
    finished = False
    while not finished:
        x=int(input("Which pile would you like to take from?(1 or 2)"))
        y=int(input("How many would you like from pile "+ str(x)+ "? "))
        if pile1 and pile2<y:
            print("pile " +str(x)+ " does not have that many chips. Try again.")
        elif y==0:
            print("You must take at least one chip. Try again.")
        else:
            print("That was a legal move. Thank You.")
        finished = True
    return x,y

def getPiles(pile1, pile2, move):
    print("Here are the piles: ")
    x,y = move
    if x == 1:
        pile1= pile1- y
        print("pile 1: ", str(pile1))
        print("pile 2: ", str(pile2))
    elif x == 2:
        pile2= pile1- y
        print("pile 1: ", str(pile1))
        print("pile 2: ", str(pile2))
        return pile1, pile2

def getCompMove(x,y, pile1, pile2):
    print("Now it's my turn.")
    pile1, pile2= pile1, pile2
    x= x 
    y= y
    if x==1:
        print("I, the champion chips computer will take "+str(y)+ " chips from pile 2")
        pile2= pile2 - y
        pile1= pile1
    elif x==2:
        print("I, the champion chips computer will take "+str(y)+ " chips from pile 1")
        pile1= pile1 - y
        pile2= pile2
    if pile1==0 and pile2==0:
        print("The game is over because I took the last chip.")
        print("Thanks for playing. Let's wager next time.")        
    return x,y, pile1, pile2

def compPiles(pile1, pile2):
    print("Here are the piles: ")
    pile1, pile2= pile1, pile2
    print("pile 1: ", str(pile1))
    print("pile 2: ", str(pile2))
    return pile1, pile2

主要

chips = int(input("How many chips would you like to start with? "))

pile1, pile2= initGame()

display = displayPiles(pile1, pile2)

move= getHumanMove(pile1, pile2)

pile= getPiles(pile1, pile2, move)

x,y = move

_,_,pile1, pile2 = getCompMove(x,y, pile1, pile2)

pile1, pile2 =pile1, pile2

compi= compPiles(pile1, pile2)

当我运行它时,它正确地消除了被移除的芯片数量,但是当计算机移动时它不会更新它。

Here are the piles: 
pile 1:  9
pile 2:  12
Now it's my turn.
I, the champion chips computer will take 3 chips from pile 2
Here are the piles: 
pile 1:  12
pile 2:  9

1 个答案:

答案 0 :(得分:0)

更新丢失并不奇怪:您将getCompMove的结果放在变量pile中,以后再也不会使用它。我想如果你添加

pile1, pile2 = pile

pile1, pile2 = getCompMove(...)

它会表现得更好。顺便说一下,当你学习Python时,你应该尝试将所有单独的函数转换为包含两个堆的类中的方法:它会更清晰,你可以使用更少的变量来传入和传出。除非在评论中声明,否则系统地隐藏具有相同名称的本地变量对读者来说是痛苦的。