-1

I keep getting this error:

IndentationError: unindent does not match any outer indentation level on line 8
    def salary_emp(self):

    ^

How can it match if I just defining function? Thanks in advance.

import math

class employee: 
    def __init__(self, name, lname, age):
        self.name = name
        self.lname = lname
        self.age = age
    def salary_emp(self):
        print("{}".format((self.age - 25)*1000 + 50000))

empl1 = employee("Will", "Brown", 40)
salary_emp(empl1)

print(empl1)
print(empl1.name)
print(empl1.lname)
Julien
  • 13,986
  • 5
  • 29
  • 53
Artin
  • 11
  • 4
  • This code does not have the error you describe. Edit your question to adjust the formatting so it is the same as your real code. – John Gordon Aug 22 '17 at 02:52
  • The only error I can spot is that you are calling `salary_emp` incorrectly. Instead, you should run that function as `empl_1.salary_emp()` – Carlo Mazzaferro Aug 22 '17 at 02:53
  • You're mixing tabs and spaces. Stop mixing tabs and spaces, and the problem will go away. – user2357112 Aug 22 '17 at 02:53
  • 2
    @user2357112 If that were the case, wouldn't the error be `TabError: inconsistent use of tabs and spaces in indentation`? – John Gordon Aug 22 '17 at 02:54
  • try to run `sed -i l your.py` to see if there is something wrong. – Will Aug 22 '17 at 03:03
  • @JohnGordon: You'd think so, but no. Take a look in the edit view. – user2357112 Aug 22 '17 at 03:04
  • @user2357112 Right you are :-) – John Gordon Aug 22 '17 at 03:08
  • Like with many other such duplicates, the problem becomes readily apparent if we [check the raw source of OP's original attempt at the post](https://stackoverflow.com/revisions/19979614-1b06-4ba8-92cd-492b854aa50a/view-source). The editor and/or Markdown processor here convert tabs to spaces. – Karl Knechtel Jul 14 '23 at 19:57

2 Answers2

1

One Question: What editor are you using?

Anyways, what usually happens is either you are using a combination of spaces and tabs or while trasferring/pasting code from one file/editor to another, the code tends to misplace itself. And this would cause sequential errors of the same type.

What usually works for me is:

Whichever line is throwing the exception, go to its beginning and backspace it till it comes in the same line as the previous line of code. Now hit enter/return.

Or, Replace all spaces by tabs. Usually a good practice.

The exception throwing line should automatically go to its correct indented place.

Ravi Raj
  • 69
  • 1
0

As @user2357112 pointed out, you're mixing tabs and spaces in the function definitions.

The lines for __init__() have leading tabs followed by spaces, but the lines for salary_emp() have only spaces.

Don't do this. Use all tabs or all spaces.

John Gordon
  • 29,573
  • 7
  • 33
  • 58