1

I am having trouble being able to output the card values from within a function (it doesn't update the variables outside the function. I've tried using return but to no avail.

When I run this, it give the name as "test" and the value as 0 (as i set it at the start.) How do I make it so it updates?

I want to be able to print the cardName after the function is finished.

Shephard_
  • 31
  • 4
  • 1
    `x, y = random_card` inside the function `return cardValue1, cardName` – Trenton McKinney Sep 08 '20 at 18:47
  • 2
    You cannot use the `return` statement twice inside the same function. Use [`yield`](https://www.geeksforgeeks.org/python-yield-keyword/) instead – neuops Sep 08 '20 at 18:48
  • Does this answer your question? [How can I return two values from a function in Python?](https://stackoverflow.com/questions/9752958/) – Trenton McKinney Sep 08 '20 at 18:50
  • Design suggestion: `random_card` shouldn't worry about the *value* of the card; it should simply return a card. It's up to whoever *called* `random_card` to decide what the card is worth (as that can change after receiving a new card) – chepner Sep 08 '20 at 18:57

3 Answers3

2

Return a tuple of the two values. You can even unpack these immediately into two variables.

>>> def f():
...     x = 5
...     y = 6
...     return (x,y)
... 
>>> f()
(5, 6)
>>> a, b = f()
>>> a
5
>>> b
6
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
0

I updated your example to work in place. You have to return both variables and can also assign them on the return:

cardName = "test"
cardValue1 = 0

def random_card():
    number = (random.choice(cardsList))
    suit = (random.choice(suitList))
    cardName = number + suit
    print (cardName)
    if cardName.startswith('A'):
        print ("Do you want this card to count as a 1 or 11?")
        oneOrEleven = input()
        if oneOrEleven == ("11"):
            cardValue1 = 11
        else:
            cardValue1 = 1
    elif cardName.startswith("2"):
        cardValue1 = 2
    elif cardName.startswith("3"):
        cardValue1 = 3
    elif cardName.startswith("4"):
        cardValue1 = 4
    elif cardName.startswith("5"):
        cardValue1 = 5
    elif cardName.startswith("6"):
        cardValue1 = 6
    elif cardName.startswith("7"):
        cardValue1 = 7
    elif cardName.startswith("8"):
        cardValue1 = 8
    elif cardName.startswith("9"):
        cardValue1 = 9
    elif cardName.startswith("1"):
        cardValue1 = 10
    elif cardName.startswith("K"):
        cardValue1 = 10
    elif cardName.startswith("Q"):
        cardValue1 = 10
    elif cardName.startswith("J"):
        cardValue1 = 10
    else:
        a = 1
    
    print ("Your card is '",cardName,"'","and it is worth",cardValue1)

    return cardValue1, cardName
    
    
cardName, cardValue1 = random_card()

print (cardName)
print (cardValue1)
james-see
  • 12,210
  • 6
  • 40
  • 47
0

You can return multiple things as once, just separate by commas and keep track of index.

def hi():
    x, y = 5, 4
    return x, y

x, y = hi()
Cleve Green
  • 729
  • 5
  • 12