Jump to content

Recommended Posts

Posted

Hi,

I have got the following program:

class TestIndexInArray:
        def __init__(self, arr):
        '''
        @param arr: array to be tested
        '''
                self.arr = arr

        def printItemsIndex(self):
                '''
                Prints the index of each item in a given array
                '''
                # message to tell us when the function was called
                print(f'Print indexes for {self.arr}:')
                for i in self.arr:
                        # find the index of i in arr
                        itemIndex = self.arr.index(i)
                        print(itemIndex)
                print('\n')


        def findItemAtIndex(self, index):
        '''
        @param index: refers to the location of the item in the array
        '''
                print(f'Item at {index} in {self.arr}:')
                try:
                        print(self.arr[index])
                except IndexError:
                        print(f'> error: index at {index} does not exist')
                print('\n')


if __name__ =="__main__":
    def header(num):
        print(f'Test #{num}\n' + '='*40 + '\n')

    header(1)
    arr_one = TestIndexInArray([4, 9, 12, 3, 5, 24])
    arr_one.printItemsIndex()
    arr_one.findItemAtIndex(0)
    arr_one.findItemAtIndex(2)
    arr_one.findItemAtIndex(5)
    arr_one.findItemAtIndex(6)

    header(2)
    arr_two = TestIndexInArray([])
    arr_two.printItemsIndex()
    arr_two.findItemAtIndex(0)

I am getting following errors:

Quote

 

File “/home/zulfi/PycharmProjects/Classes/exceptionProg.py”, line 3
‘’’
^
TabError: inconsistent use of tabs and spaces in indentation
Process finished with exit code 1

 

 

Somebody please guide me.

Zulfi.

Posted

In python you must use either tabs or spaces for indentation not both. It is an annoying feature of python if you are using notepad++ you can "view>show symbol>show white space and tabs"

Posted

Hi @fiveWorlds and @Strange,

Thanks for helping me.

I tried cutting and then pasting on notepad and then pasting it back on the pycharm but sorry it did not work. However when I clicked the bulb it asked me to convert the tabs into spaces and this worked.

Thanks a lot.

 

Zulfi.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.