2

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 = '\033[1m'
        OKGREEN = '\033[92m'
        FAIL = '\033[91m'
        ENDC = '\033[0m'
        YELLOW = '\033[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'
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
Alex
  • 25
  • 5
  • 1
    Your functions are defined `__init__` and aren't properties of the class. Unindent those functions – 12944qwerty Nov 10 '22 at 13:27
  • 1
    1. `__init__` is called when you create the object in `initial = SpellCheck()`. 2. all you class functions are indented too much and are instead defined inside `__init__`. – ivvija Nov 10 '22 at 13:27
  • More than that you shouldn't call __init__ after you already write SpellCheck(). __init__ will be called automatically when you create instance of SpellCheck class – puf Nov 10 '22 at 13:30
  • You could try an IDE like PyCharm or VS code. Both are free, but VS code needs more configuration. It will help you by marking your errors. – PythonForEver Nov 10 '22 at 13:42

1 Answers1

3

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.

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44