Alexa
Alexa

Reputation: 11

TypeError: 'int' object does not support item assignment?

Why am I getting this error?

    playerHand[1] = randint(1,11)
TypeError: 'int' object does not support item assignment

Here's the code. I think it looks fine, but I may have some pretty bad mistakes. sorry for the dumb question:

print ("********* WELCOME TO A BLACKJACK GAME *********")

endProgram = False


from random import randint

computerHand1 = randint(1,11)
computerHand2 = randint(1,11)

print ("Dealer's cards: " + "X and " + str(computerHand2))

totalcomputerHand = computerHand1 + computerHand2

playerHand = randint(1,11)

playerHand[1] = randint(1,11)
playerHand[2] = randint(1,11)

print ("Your cards are: " + str(playerHand[1]) + " , " + str(playerHand[2]))

while endProgram == False:   
    totalplayerHand = playerHand[1] + playerHand[2] + playerHand[3] + playerHand[4] + playerHand[5] + playerHand[6] + playerHand[7] + playerHand[8] + playerHand[9] + playerHand[10] + playerHand[11] + playerHand[12] + playerHand[13] + playerHand[14] + playerHand[15] + playerHand[16] + playerHand[17] + playerHand[18] + playerHand[19] + playerHand[20] + playerHand[21]

    if totalplayerHand == (21):
        if totalcomputerHand == (21):
            endProgram = True
            print ("Dealer's hand equals 21 and your hand equals 21. This is a draw")
        if totalcomputerHand < (21):
            endProgram = True
            print ("Dealer's hand equals " + str(totalcomputerHand) + " and your hand equals 21. You have won the game")

    counter = 3

I am trying to do arrays which are lists in python (for the first time), and I am also new to coding and python.

Upvotes: 1

Views: 4593

Answers (6)

Anthony Pham
Anthony Pham

Reputation: 3106

When you are have:

playerHand = randint(1,11)

playerHand is now a variable correct? When you attempt:

playerHand[1] = randint(1,11)
playerHand[2] = randint(1,11)

You are treating the variable playerHand as a list now, creating the error. To prevent this error, you probably need to make playerHand a list instead like this:

playerHand = []

to prevent the error. You can also do this:

playerHand1 = randint(1, 11)
playerHand2 = randint(1, 11)

You can use @Wally Beaver's answer but you need to fix one thing. From this:

playerHand[1] = randint(1,11)
playerHand[2] = randint(1,11)

to this:

playerHand[0] = randint(1,11)
playerHand[1] = randint(1,11)

Why? Because all lists start with 0, not 1. If you don't do this, another error will appear. To add stuff to your list, do this instead:

playerHand1 = randint(1, 11)
playerHand2 = randint(1, 11)
playerHand.append (playerHand1)
playerHand.append (playerHand2)

This will get rid of your list assignment error. You can see how by adding this right after:

print playerHand1, playerHand2

I hope this helps you!

Upvotes: 1

user4482921
user4482921

Reputation:

This is an int (not a list):

playerHand = randint(1,11)

To make a list you want to start like this:

playerHand = []

Then, you can assign the values to the indices, as you are trying to do:

playerHand[1] = randint(1,11)
playerHand[2] = randint(1,11)

Although, python starts the list indices with 0, not 1.

Note that also the easiest way to add new items to a list the first time is with extend or append:

playerHand.append(randint(1,11))

Indexing only works when you already have items in the list -- where you can give an index less than len(playerHand).

Upvotes: 1

Isvaldo Fernandes
Isvaldo Fernandes

Reputation: 92

You need make a list.

The wrong version:

    playerHand = randint(1,11) ## return list
    playerHand[1] = randint(1,11) ## playerHand is not a list
    playerHand[2] = randint(1,11)

The correct version:

playerHand = []

playerHand[1] = randint(1,11) ## playerHand is a list !!
playerHand[2] = randint(1,11)

Upvotes: 1

hamsternik
hamsternik

Reputation: 1426

This code have some wrong moments, for example, in lines:

while endProgram == False:   
totalplayerHand = playerHand[1] + playerHand[2] + playerHand[3] + playerHand[4] + playerHand[5] + playerHand[6] + playerHand[7] + playerHand[8]

So, you don't have any values into playerHand array, except first two.

And make playerHand as array, 'cause now you have an integer number.

Upvotes: 0

James Mishra
James Mishra

Reputation: 4500

The problem is that you set playerHand to a number.

playerHand = randint(1,11)

If you type the above line into a terminal, you will find that it is a number and not a list.

playerHand[1] = randint(1,11)
playerHand[2] = randint(1,11)

The kind of assignment you are doing is here, with the [1] and [2] is for list or dictionary elements (and other ones that support item assignment as your error message explains).

You probably meant to do something like

computerHand1 = randint(1,11)
computerHand2 = randint(1,11)

in which case the proper code would be

playerHand1 = randint(1,11)
playerHand2 = randint(1,11)

Now... is this really the right way to structure the problem as a whole? I think we can do better in terms of data structures, but this small adjustment will get rid of your error.

Upvotes: 1

aruisdante
aruisdante

Reputation: 9065

Here:

playerHand = randint(1,11)

You assign playerHand the return from randint, which is an int. Then here:

playerHand[1] = randint(1,11)

You attempt to index into playerHand like it is an array. Hence the error.

Upvotes: 1

Related Questions