Alanan
Alanan

Reputation: 31

'int' object is not subscriptable in my code

I'm trying to write a program in Python that will compare two 4-digit numbers, one given by the player and one generated by the computer, and tell the player how many cows and bulls they get - cows being matching numbers in the wrong places and bulls being matching numbers in the right places. I keep getting an 'int' object is not subscriptable error every time I try it. I know that means I need to make the int guess a string, but even when I try to do that it gives me the same error in the same place. Any pointers?

def how_many_bulls(answer,guess):
    ''' Returns the number of bulls the guess earns when the secret number
    is the answer. '''
    bulls = 0
    if guess[0] == answer[0]:
        bulls = bulls+1
        if guess[1] == answer[1]:
            bulls = bulls+1
            if guess[2] == answer[2]:
                bulls = bulls+1
                if guess[3] == answer[3]:
                    bulls = bulls+1
    return bulls

Sorry if I've formatted anything wrong - my first time using the site.

Upvotes: 2

Views: 70

Answers (2)

Ella Sharakanski
Ella Sharakanski

Reputation: 2773

You should convert to string both answer and guess.

(Or access their digits in any other way, there are many)

Upvotes: 2

eeScott
eeScott

Reputation: 193

You can make the guess a list:

guess = [1,2,3,4]

Upvotes: 0

Related Questions