Reputation: 23
I'm trying to insert a card into the player list. Here's my code and the error I get:
def deal_card():
"""Returns a random card from the deck."""
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
card = random.choice(cards)
return card
player = deal_card()
print(type(player))
player.insert(deal_card())
print(player)
AttributeError: 'int' object has no attribute 'insert'
I've tried .append
and .add
. Why does my code throw this error and how do I fix it?
What I want is [ 2, 3, 5]
Upvotes: 0
Views: 650
Reputation: 69
this error means you are trying to call insert()
method which owned by list data type, while the data type you are trying to use is an Integer. In your deal_card
function,
def deal_card():
"""Returns a random card from the deck."""
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
card = random.choice(cards)
return card
card = random.choice(cards)
will give choose a random value from your cards
list, in which will return a whatever type that is chosen in cards
list, in your cases is an Integer type.
note that insert() must have 2 parameters to be passed for further information, you can read here
if you want to make a list of the chosen random cards i would recommend to use append()
and you can try to use a different attribute maybe called card
for passing the data, or just directly append the deal_card
function since its return a value, here's an example:
# to use append you should already have a list whether its empty or not
player = []
card = deal_card()
# can also use player.append(deal_card()), in that case you won't need the card variable
player.append(card)
Upvotes: 0