Indentation error in my code because of the text editor?

So, I was making an exercise, but it didn’t work and showed an error. I decided to try on a different platform. I copied my code and pasted it, and found out that the code looked like this:

class Patient:
	def __init__(self, name, age, sex, bmi, num_of_children, smoker):
		self.name = name
		self.age = age
    # add more parameters here
    self.sex = sex
    self.bmi = bmi
    self.num_of_children = num_of_children
    self.smoker = smoker

However, this doesn’t make any sense, because at codecademy it is displayed like this:

class Patient:
	def __init__(self, name, age, sex, bmi, num_of_children, smoker):
		self.name = name
		self.age = age
                # add more parameters here
                self.sex = sex
                self.bmi = bmi
                self.num_of_children = num_of_children
                self.smoker = smoker

edit: well, it seems it also happens here. But in the second code, all the parameters are supposed to be assigned in the same indentation.

Both of those look problematic, can you provide details of the exact error you’re getting. I’m assuming all those terms should have the same indentation? Is there perhaps a mixture of tabs and spaces which is causing the changes when displayed on different platforms? Inconsistent use of tabs and spaces is a nightmare for reasons like this. Adjust your code for the correct indentation, try using only spaces and an perhaps a code editor that automatically converts tabs to spaces for the best results.

Edit: I shifted this into Python since that’s the language used.

1 Like

I’ve been trying, and using only spaces didn’t work at first. I tried putting everything together ad separating them with only spaces. That didn’t work neither, but now I just got it right randomly.

@tgrtim is likely right about the tabs/spaces. If I get that error, I take everything back to no indents, and then use only tab to get it back to the right level of indentation.

2 Likes

Yes, exactly.

Regarding the following lines from the original post, the first has one tab as its indentation, and the second and third each have two tabs:

	def __init__(self, name, age, sex, bmi, num_of_children, smoker):
		self.name = name
		self.age = age

All the other indentations in the program are composed entirely of spaces. You can gradually use the mouse or another technique to select that whitespace in order to observe the size of the selectable units, so that the composition of each of the indentations becomes evident.

Whenever an IndentationError occurs despite the fact that the indentation appears correct, it is a good idea to select the whitespace to investigate its composition.

1 Like

I’m still not understanding what the solution to this problem is. I’ve tried moving everything to the left side fo the screen and indenting myself. I’ve also tried using spaces. But I can’t seem to get this code to work. Please help me. It’s for the same Python Classes: Medical Insurance Project santiagoqin was having trouble with.

Could you post a link please? When you moved everything to the left, did you move even the already given code to the left?

Hey guys had the same issue and tried everything: deleting the indents and then manually using tab to create new ones, the same but with spaces instead etc. What fixed it for me was removing the indent before the init method, running, and then adding the indent back again and running. Have no idea, why it works but it doesn’t throw up the indentation error now.

1 Like