If-else statement not functioning properly in for loop

Question:

I’m trying to count the different characters in two individual strings using an if-else statement in a for-loop. However, it never counts the different characters.

        for char in range(len(f1CurrentLine)):  # Compare line by line
            if f1CurrentLine[char] != f2CurrentLine[char]:  # If the lines have different characters
                print("Unmatched characters: ", count, ":", char)
                diffCharCount = diffCharCount + 1  # add 1 to the difference counter
                count = count + 1
                text1Count = text1Count + len(f1CurrentLine)
                text2Count = text2Count + len(f2CurrentLine)
                return CharByChar(count=count, text2Count=text2Count, text1Count=text1Count,
                                   diffCharCount=diffCharCount)  # return difference count
            else:
                print("Characters matched in line:", count, ". Moving to next line.")
                text1Count = text1Count + len(f1CurrentLine)
                text2Count = text2Count + len(f2CurrentLine)
                count = count + 1
                return CharByChar(count, diffCharCount=diffCharCount, text1Count=text1Count,
                                   text2Count=text2Count,
                                   diffLineCount=diffLineCount)

I have two files with the following in them

File 1:

1 Hello World

2 bazzle

3 foobar

File 2:

1 Hello world

2 bazzle

3 fooBar

It should return 2 different characters, but it does not. If you want to take a look at the entire function I have linked it here: Pastebin. Hopefully you can see something I have missed.

Asked By: user9722579

||

Answers:

Your code is too complicated for this sort of application. I’ve tried my best to understand the code and I’ve come up with a better solution.

text1 = open("file1.txt")
text2 = open("file2.txt")

# Difference variables
diffLineCount = diffCharCount = line_num = 0


# Iterate through both files line by line
for line1, line2 in zip(text1.readlines(), text2.readlines()): 
    if line1 == "n" or line2 == "n": continue # If newline, go to next line
    if len(line1) != len(line2): # If lines are of different length
        diffLineCount += 1
        continue # Go to next line
    for c1, c2 in zip(list(line1.strip()), list(line2.strip())): # Iterate through both lines character by character
        if c1 != c2: # If they do not match
            print("Unmatched characters: ", line_num, ":", c1)
            diffCharCount += 1
    line_num += 1

# Goes back to the beginning of each file
text1.seek(0)
text2.seek(0)

# Prints the stats
print("Number of characters in the first file: ", len(text1.read()))
print("number of characters in the second file: ", len(text2.read()))
print("Number of characters that do not match in lines of the same length: ", diffCharCount)
print("Number of lines that are not the same length: ", diffLineCount)

# Closes the files
text1.close()
text2.close()

I hope you understand how this works and are able to make it fit your needs specifically. Good luck!

Answered By: kpaul

Unlike the other solution I edited your code, so that you can understand what was going wrong. I agree with him that you should anyway organize better your code because it is complex

text1 = open("file1.txt")
text2 = open("file2.txt")


def CharByChar(count, diffCharCount, text1Count, text2Count, diffLineCount):
    """
    This function compares two files character by character and prints the number of characters that are different
    :param count: What line of the file the program is comparing
    :param diffCharCount: The sum of different characters
    :param text1Count: Sum of characters in file 1
    :param text2Count: Sum of characters in file 2
    :param diffLineCount: Sum of different lines
    """
    # see comment below for strip removal
    f1CurrentLine = text1.readline()
    f2CurrentLine = text2.readline()

    while f1CurrentLine != '' or f2CurrentLine != '':
        count = count + 1
        print(f1CurrentLine)
        print(f2CurrentLine)
        #if f1CurrentLine != '' or f2CurrentLine != '':
        if len(f1CurrentLine) != len(f2CurrentLine):  # If the line lengths are not equal return the line number
            print("Lines are a different length. The line number is: ", count)
            diffLineCount = diffLineCount + 1
            count = count + 1
            #text1Count = text1Count + len(f1CurrentLine)
            #text2Count = text2Count + len(f2CurrentLine)
            # return CharByChar(count)
        elif len(f1CurrentLine) == len(f2CurrentLine):  # If the lines lengths are equal    
            for char in range(len(f1CurrentLine)):  # Compare line by line
                print(char)
                if f1CurrentLine[char] != f2CurrentLine[char]:  # If the lines have different characters
                    print("Unmatched characters: ", count, ":", char)
                    diffCharCount = diffCharCount + 1  # add 1 to the difference counter
                    #count = count + 1
                    text1Count = text1Count + len(f1CurrentLine)
                    text2Count = text2Count + len(f2CurrentLine)
                    # return CharByChar(count=count, text2Count=text2Count, text1Count=text1Count,diffCharCount=diffCharCount)  # return difference count
                else:
                    print("Characters matched in line:", count, ". Moving to next char.")
                    #text1Count = text1Count + len(f1CurrentLine)
                    #text2Count = text2Count + len(f2CurrentLine)
                    #count = count + 1
                    #return CharByChar(count, diffCharCount=diffCharCount, text1Count=text1Count,text2Count=text2Count,diffLineCount=diffLineCount)
        #elif len(f1CurrentLine) == 0 or len(f2CurrentLine) == 0:
            #print(count, "lines are not matching")
            #diffLineCount = diffLineCount + 1
            #return CharByChar(diffLineCount=diffLineCount)
        else:
            print("Something else happened!")

        f1CurrentLine = text1.readline()
        f2CurrentLine = text2.readline()

    print("Number of characters in the first file: ", text1Count)
    print("number of characters in the second file: ", text2Count)
    print("Number of characters that do not match in lines of the same length: ", diffCharCount)
    print("Number of lines that are not the same length: ", diffLineCount)


def main():
    "Calls the primary function"
    CharByChar(count=0, diffCharCount=0, text1Count=0, text2Count=0, diffLineCount=0)
    input("Hit enter to close the program...")


main() #Runs this bad boi
  • I think the general trouble is organizing your CharByChar() function to scan all the lines in the file [which is something we maintain in this solution] but then asking to call the same function at then end of every character check
  • some parts have no reasons to be there: for example you set count in the main when calling CharByChar() and then you create a branch with if(count == 0). You can cut this out, the code will look cleaner
  • some variables as well should be removed to keep the code as clean as possible: you never use text1Count and text2Count
  • you enter with a condition on the while and the next if has the same condition: if you entered the while you will enter also the if [or none of them] so you can cut one of them out
  • I suggest you to remove the branch with if len(f1CurrentLine) == 0 or len(f2CurrentLine) == 0 because both the files can have length 0 for the same line and then the lines would be equal [see the very next example below]
  • I suggest you to remove the strip() to avoid troubles to interrupt the check earlier for files where you have newlines in the middle, e.g.
    1 Hello
    
    3 foobar
    
Answered By: Antonino
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.