Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Solution:
I changed the comparison to:
Python
if int(guess[i]) == number[i]:


Full Program:
Python
import random

def generateNumbers():
    numbers = [];
    i = 0
    while i < 4:
        newNumber = random.randint(0, 9)
        if numbers.count(newNumber) == 0:
            numbers.append(newNumber)
            i += 1
    return numbers

def checkValidity(guess):
    if len(guess) != 4:
        print("Your guess does not contain four numbers.")
        return False
    if guess.isdigit() != True:
        print("Your guess is not a number.")
        return False
    i = 0
    while i < 4:
        if guess.count(guess[i]) != 1:
            print("Your number contains a duplicate.")
            return False
        i += 1
    return True

def checkMatch(guess, number):
    bulls = 0
    cows = 0
    i = 0
    while i < 4:
        if guess[i] == number[i]:
            bulls += 1
        elif guess[i] in number:
            cows += 1
        i += 1
    print("Your number contains " + str(bulls) + " and " + str(cows) + " cows.")
    if bulls == 4:
        return True
    else:
        return False

number = generateNumbers()
print(number)
guess = ""
guesses = 0
while guess != "exit":
    print("Enter your guess or 'exit'.")
    guess = input("Guess: ").lower()
    if guess == "exit":
        break
    if checkValidity(guess):
        guesses += 1
        if checkMatch(int(guess), number):
            print("Well done! You guessed the number in " + str(guesses) + " tries.")


Hi there, I am want to compare two ints together in the following code:
Python
if guess[i] == number[i]:
However, I am getting the following error:
TypeError: 'int' object is not subscriptable


What I have tried:

- Converting the variable "guess" into an int in different places of the code.
- Not converting it at all.
Posted
Updated 27-Apr-17 9:06am
v4

guess is not an [] - so it can;t be used with a subscript index.
Try:
if guess == number[i]:
 
Share this answer
 
Comments
Yaseen M 27-Apr-17 14:10pm    
That would defeat the purpose of the original code. What I want to do is iterate through every digit of the number the user had entered.
Quote:
That would defeat the purpose of the original code.

Do the correction of solution 1, and use the debugger to see what your code is doing. Somewhere, your code is not doing what you expect.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900