Indentation error on "Taking a vacation" - 3

https://www.codecademy.com/courses/learn-python/lessons/taking-a-vacation/exercises/getting-there?action=resume_content_item
^ Is the task I am currently on. For some reason the exact same solution from codecademy works, but when I do it myself it throws an indentation error. I cannot see what I’m doing wrong

Codecademy solution:

def hotel_cost(nights):
  return 140 * nights

def plane_ride_cost(city):
  if city == "Charlotte":
    return 183
  elif city == "Tampa":
    return 220
  elif city == "Pittsburgh":
    return 222
  elif city == "Los Angeles":
    return 475

My code:

def hotel_cost(nights):
	return 140 * nights

def plane_ride_cost(city):
	if city == "Charlotte":
  	return 183
  elif city == "Tampa":
  	return 220
  elif city == "Pittsburgh":
  	return 222
  elif city == "Los Angeles":
  	return 475

Edit: looking at it on here, I can see that "if city == “charlotte” is indented twice in the second code that I created, however, on the code editor on codecademy they both looked identical, but when pasting it on here I can see where the error in the code is.

1 Like

Seems you already solved the issue yourself?

1 Like

Indeed I have, however, I’m still confused as to why the indentation looked perfectly fine on the code editor, however, when pasted on here, the identation errors were clear to see.

A educated guess would be to say that web-based IDE’s are very complicated, maybe a problem with tab/spaces configuration? soft vs hard tabs

2 Likes

That was my thoughts too, it’s something I will need to be more careful about in the future. What type of indents would you recommend me use? Tabbing or using the spacebar to indent?

i recommend a better text-editor/IDE in the future :wink:

i recommend using soft-tabs, this will insert spaces when the tab key is pressed.

1 Like

Thank you for your help. This thread is now solved.

Cheers :slight_smile:

1 Like

Personally, because they’re unambiguous - spaces.

This also opens a whole other can of worms - 2 vs 4 spaces… :laughing:

You know pep8? they are the recommend coding style guide for python, you must be a very stubborn program to go against them. pep8 clearly states the use of 4 spaces.

Yes I am familiar with PEP8, but nowhere did I say how many spaces I use - simply that for consistency I use them rather than tab, and that you can easily find debate amongst coders over whether to use 2 spaces or 4.

Consistency being the key word here, of course. After all, PEP8 is a style guide and not an immutable set of commandments. Provided the code is consistently readable, I don’t think it really matters. :+1:

the pep8 on indention:

https://www.python.org/dev/peps/pep-0008/#indentation

the first line is pretty explicit:

Use 4 spaces per indentation level.

sure, style guides aren’t mandatory but these guidelines do exist for a reason. So it leaves some room for debate.

True, but the guide does also say:

A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is the most important.

However, know when to be inconsistent – sometimes style guide recommendations just aren’t applicable. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don’t hesitate to ask!

Granted, I can’t off the top of my head justify how 2-spaces is more readable than 4-spaces (unless you’re constained for screen space, and have some horrific nesting going on maybe?), but my point remains that PEP8 is a fits-most-cases “best practice” guideline rather than “do it or else” rules. :slight_smile:

2 Likes

To tell the truth, I’ve never before encountered an editor that uses 2 spaces. And it certainly makes it difficult to copy-and-paste CA code into an IDE that complains about indentation. This has been complained about before in the forum, the consensus seeming to be that trying to squeeze three columns into a screen demanded a smaller default indentation, as four spaces would make too many lines run over, which would look worse.

The consensus also seems to be that no one is going to do anything about it.

For what it’s worth, I would prefer an “instructions in the left 1/3, with code and console in an over-under configuration in the right 2/3” setup.

capture

1 Like

Your diagram is pretty much the configuration of my preferred editor… with the project file tree in place of the instructions.

I’ve also never come across an editor or IDE that defaulted to 2 when using spaces, it’s always been 4. (That’s also generally my preference.)

I’ve never understood the need for 4 spaces, but nobody asked me before they wrote PEP8 or any other style guide. How are the 2 extra spaces anything other than a waste of space? That said, I believe in following commonly accepted standards, but if they ever put it to a vote…