AttributeError, What am I doing wrong here?

Question:

I have a program that I cannot call to run, where I try to call the linearSearch() def from spellCheck.py. Can someone help me understand why my code gives me a AttributeError? I do not understand why when calling initial.linearSearch(choice) won’t give me anything.

spellCheck.py:

class SpellCheck():
    def __init__(self):
        try:
            open("dictionary.txt", "r")
        except FileNotFoundError:
            print("Dictionary file not found")
        else:
            #store all elements in a list 
            dictionary = []
            #open dictionary.txt
            with open("dictionary.txt", "r") as f:
                for line in f:
                    dictionary.append(line.strip())
        def binarySearch(self, word):
            steps = 0
            low = 0
            high = len(dictionary) - 1
            while low <= high:
                middle = (low + high) // 2
                if(dictionary[middle] == word):
                    steps += 1
                    return (f"{bcolors.OKGREEN}Found {bcolors.BOLD}{word}{bcolors.ENDC}{bcolors.OKGREEN} after {steps} steps!{bcolors.ENDC}")
                elif (dictionary[middle] < word):
                    steps += 1
                    low = middle + 1
                else:
                    steps += 1
                    high = middle - 1
            return(f"{bcolors.FAIL}The word {bcolors.BOLD}{word}{bcolors.ENDC}{bcolors.FAIL} wasn't found!{bcolors.ENDC}")

        def linearSearch(word):
            steps = 0
            for i in range(len(dictionary)):
                steps += 1
                if dictionary[i] == self.word:
                    steps += 1
                    return(f"{bcolors.OKGREEN}Found {bcolors.BOLD}{self.word}{bcolors.ENDC}{bcolors.OKGREEN} after {steps - 1} steps!{bcolors.ENDC}")
            return(f"{bcolors.FAIL}The word {bcolors.BOLD}{self.word}{bcolors.ENDC}{bcolors.FAIL} wasn't found!{bcolors.ENDC}")

#color coding for terminal
#source: https://stackoverflow.com/a/287944
#either True or False
class bcolors:
        BOLD = '33[1m'
        OKGREEN = '33[92m'
        FAIL = '33[91m'
        ENDC = '33[0m'
        YELLOW = '33[93m'
        #debug statement
        #if debug == True:
            #print(f"Debug Colors:n{BOLD}BOLD{ENDC}n{OKGREEN}OKGREEN{ENDC}n{FAIL}FAIL{ENDC}n{YELLOW}YELLOW{ENDC}")
    #end of color codes


main.py

from spellCheck import SpellCheck
#from spellCheck import bcolors

def main():
    choice = input("Enter the word to look for:n> ")
    initial = SpellCheck()
    initial.__init__()
    initial.linearSearch(choice)
    
    
main()

Here is the output of the terminal:

Enter the word to look for:
> apple
Traceback (most recent call last):
  File "main.py", line 11, in <module>
    main()
  File "main.py", line 8, in main
    initial.linearSearch(choice)
AttributeError: 'SpellCheck' object has no attribute 'linearSearch'
Asked By: Alex

||

Answers:

binarySearch(self, word) and linearSearch(word) are the functions of __init__

that’s why you are not getting any error on initial.linearSearch(choice).

If you want them to be separate functions of SpellCheck() then unindent them.

Answered By: God Is One