This question already has answers here:
What's the best way to return multiple values from a function in Python?
                                
                                    (5个答案)
                                
                        
                                5年前关闭。
            
                    
我是python的新手,所以我还在学习一些东西。但是我正在执行此代码,我需要知道如何从“ getHumanMove”函数获取x和y变量,以便可以在“ getPiles”函数中使用它

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):
    print("Here are the piles: ")
    #################################Here is where I need to put the x, y variables
    pile1= pile1
    pile2= pile2
    if temp_x == 1:
        pile1= pile1- temp_y
        print("pile 1: ", str(pile1))
        print("pile 2: ", str(pile2))
    elif temp_x == 2:
        pile2= pile1- temp_y
        print("pile 1: ", str(pile1))
        print("pile 2: ", str(pile2))
        return pile1, pile2

##############################Main##############################
move= getHumanMove(pile1, pile2)

pile= getPiles(pile1, pile2)

最佳答案

在调用getPiles时如何:

pile = getPiles(pile1, pile2, move)


然后在您的getPiles定义中:

def getPiles(pile1, pile2, move):
    x, y = move

10-08 11:59