0

I am trying to create a program that gives you 3 random "common cards" from a list to be your starter cards. Whenever I try to run it, I am given the first one fine, but then it says [None]. Is there a way to put my functions (cards) into this list, and have the program return 3 random cards?

  • I tried using random.sample(common_cards, 3)0, expecting it to give me 3 of the functions.

when I ran it, I would get(for example), jerry [None]

My code is:

import random

#this is the player deck. It *hopefully* will randomly gain 3 common cards upon running
player_deck = []

#these are cards
def orc(): 
  print("orc") 
 
def spirit(): 
  print("spirit") 
 
def bob(): 
  print("bob") 
  
def jerry():
    print("jerry")

#the cards above are "common" cards, and will be drawn as such
common_cards = [orc, spirit, bob, jerry] 

#this parts goal is to draw three of the functions as cards- No duplicates.
cards = random.sample(common_cards, 3)[0]()

#this adds the cards to be displayed in the player deck
player_deck.append(cards)

print(player_deck)

Timmy
  • 1
  • Your functions should return a value if you expect that calling one of them will result in a value other than None in the `cards` list. – mkrieger1 Jun 12 '23 at 21:31
  • `cards = random.sample(common_cards, 3)` returns a list of three random function objects, and binds that list to `cards`. `cards = random.sample(common_cards, 3)[0]` returns a list of three random function objects, accesses the 0th (first) of those three function objects, and binds that single function object to `cards`. `cards = random.sample(common_cards, 3)[0]()` returns a list of three random function objects, accesses the 0th (first) of those three function objects, executes it, and binds the return value of that function (which will be `None`) to `cards`. – Paul M. Jun 12 '23 at 21:33

0 Answers0