Jump to content

Tabs and spaces problem with Python program

Featured Replies

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.

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"

  • Author

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.

Archived

This topic is now archived and is closed to further replies.

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.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.