Python - Why is my code returning a TypeError

51 Views Asked by At

I'm new to the world of programming (as you can probably tell by my inefficient code & question).

My code is meant to check whether two subsequent characters in a string are identical. I don't understand why the two dummy variables are triggering a TypeError when being referenced in the if statement (if string[i] == string[j]:). They've been initialized as int - why does python appear to be interpreting this as a str type when being used in an if statement?

Thanks for your time.

#Get string from user
string = str(input("Enter a phrase: "))

#initialise i and the empty string doubleoccurrences
i = 0
j = i + 1

doubleOccurrences = str("N/A")

#loop through the string, which checks if two consecutive characters match
for i in string:
    if string[i] == string[j]:
        doubleOccurrences = doubleOccurrences.replace("N/A","")
        doubleOccurrences = (doubleOccurrences + string[i] + string[j])
        j = j + 1
    else:
         doubleOccurrences = doubleOccurrences
         j = j + 1

#print the output
print("Your input contains the following double occurrences: " + doubleOccurrences)


I was expecting python to interpret

if string[i] == string[j]:

as a check on whether the character at index i is equal to the character at index j, given that both i and j have been initialized as integers.

2

There are 2 best solutions below

0
Braian Pita On BEST ANSWER

When you do for i in string: you are basically overwriting your i variable with the string iterator for the variable 'string'. This means you are iterating through each letter in the string and storing the character in "i". The correct way to go about this would be to use enumerate. You can also use range like this range(len(string)). Here is a working example of what you are trying to do:

string = str(input("Enter a phrase: "))

doubleOccurrences = str("N/A")

#loop through the string, which checks if two consecutive characters match
for i in range(len(string)):
    for j in range(i + 1, len(string)):
        if string[i] == string[j]:
            doubleOccurrences = doubleOccurrences.replace("N/A","")
            doubleOccurrences = (doubleOccurrences + string[i] + string[j])
        else:
            doubleOccurrences = doubleOccurrences

#print the output
print("Your input contains the following double occurrences: " + doubleOccurrences)

Note that I am unsure what you are trying to do exactly, so I did not make sure that the result was the right result, I only made sure that it runs. I also had to modify some of your logic, as you are not accounting for 'j' going out of bounds. Using a double for loop will avoid this issue.

1
Sam Chisolm On

I understand what you're trying to do, but your Python code won't do it and definitely throw an Error, instead try this simplified code:

# Get string from user
string = input("Enter a phrase: ")
doubleOccurrences = ""

# loop through the string, which checks if two consecutive characters match
for i in range(len(string) - 1):
    if string[i] == string[i + 1]:
        doubleOccurrences += string[i] * 2
# print the output
print("Your input contains the following double occurrences:", doubleOccurrences)

To explain this code, we take input of user and store it in string, then set doubleOccurences to empty string, then we loop through 0 to len(string) - 2 inclusive (notice that character at position len(arr) - 1 doesn't have a next character after it to check for!), then we check if condition satisfied we add 2 times the character to doubleOccurrences doubleOccurrences += string[i] * 2 then finally print the result.