2

I'm very new at python so I'm still learning some things. But I'm doing this code and I need to know how to get the x and y variables from the "getHumanMove" function so that I can use it in the "getPiles" function

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)
TheAznHawk
  • 43
  • 1
  • 1
  • 6

4 Answers4

1

How about in your call to getPiles:

pile = getPiles(pile1, pile2, move)

and then in your getPiles definition:

def getPiles(pile1, pile2, move):
    x, y = move
Henry
  • 6,502
  • 2
  • 24
  • 30
0

Assuming this is what you mean:

x,y = getHumanMove(pile1, pile2)
pile1, pile2 = getPiles(x, y)
Chris Redford
  • 16,982
  • 21
  • 89
  • 109
0

You can use tuple unpacking:

move = getHumanMove(pile1, pile2)
pile = getPiles(*move)

Or get each variable separately, so you can pass them separately too:

move1, move2 = getHumanMove(pile1, pile2)
pile = getPiles(move1, move2)
Community
  • 1
  • 1
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

When a function is returning two values, you can do

value1, value2 = some_function(argument1, argument2, argument3, ...)

to store the two return values in value1 and value2.

timgeb
  • 76,762
  • 20
  • 123
  • 145