繁体   English   中英

用户在python中更改变量后,如何保持更新?

[英]How do I keep updating a variable after the user changes it in python?

我正在做一个游戏,用户输入“ x”数量的筹码放入桩1和桩2。 (例如,用户输入:12;桩1:12,桩2:12),然后从第一桩或第二桩中拿走“ 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

丢失更新并不奇怪:您将getCompMove的结果放入可变pile ,以后再也不使用。 我认为,如果您添加

pile1, pile2 = pile

要么

pile1, pile2 = getCompMove(...)

它会表现更好。 顺便说一句,在学习Python时,您应该尝试将所有单独的函数转换为包含两个堆的类中的方法:这将更清楚,并且传入和传出的变量也更少。 除非在注释中声明,否则系统地隐藏具有相同名称的局部变量的全局变量是读者的痛苦。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM