Search code examples
pythonrandomcombinatorics

How to replace a part of a function with another function?


This is my code for python:

def RandomCard():
    x=number[ran.randint(0,12)]
    y=suit[ran.randint(0,3)]
    print("Your card is the:", x, "of", y)

    
def AceOfSpades():
    count = 1
    x=number[ran.randint(0,12)]
    y=suit[ran.randint(0,3)]
    while x != 'Ace' or y != 'Spades':
        x=number[ran.randint(0,12)]
        y=suit[ran.randint(0,3)]
        count += 1
    else: 
        print("Your card is the:", x, "of", y)
        print("It took", count, "tries to get the Ace of Spades!")

how do I replace the

    x=number[ran.randint(0,12)]
    y=suit[ran.randint(0,3)]

in the AceOfSpades() fn with RandomCard() instead?

When I simply put RandomCard() in place for the x=number[ran.randint(0,12)] y=suit[ran.randint(0,3)] , it did not function as I wanted it to.


Solution

  • You can return a tuple containing x and y from RandomCard.

    def RandomCard():
        x = number[ran.randint(0,12)]
        y = suit[ran.randint(0,3)]
        print("Your card is the:", x, "of", y)
        return x, y
    

    Then, unpack it in AceOfSpades:

    x, y = RandomCard()