Getting there-Taking a vacation function undefined


#1

Here's 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=="Pittsburg":
                return 222
    else: 
        return 475

I've got:

File "python", line 6
elif city=="Tampa":
^
SyntaxError: invalid syntax

and also:
Oops, try again. Did you create a function called plane_ride_cost?

Could anyone help please? Thanks a lot.


#2

The error message from the SCT is a standard response. The real error is thrown by Python relating to the indentation. elif and else must line up with their opening if.

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

#3

Thanks a lot!!! It worked now. :joy:


#4

Tip to save future grief... Never trust auto-indent when writing Python. When you finish writing a block, go back through every line and redo the indents with 4 spaces per level. It's a pain, but it makes sure Python sees consistency.


#5

wow, that seems to be a lot work, but I guess it's still a nice habit, thank you for the tip. I'll bear that in mind.


#6

Python will let you know if something is misaligned, there's no need to manually re-do indentation.

And it did. Not extremely specific about what exactly was wrong with the syntax but it gave you a line number to stare at.

If staring at it isn't enough, I suggest opening up a second code file and copying the offending part of the code to isolate it, to make sure that the mistake really is in the couple of lines that you copied, just keep removing things until you've got the least amount of code remaining that still produces the syntax error. By now you shouldn't have much code left and you should know what you're trying to do, so if you don't recognize it, go google for some code examples containing what you're doing, in this case if-elif-else statements.

It doesn't take many mistakes like this to develop an eye for what's wrong


#7

I've found numerous instances where mixed spaces and tabs has created problems (in the past). Enough so to warrant my distrust of IDE's that don't flag mixes.

Great answer, as always. Either way we get to the same thing. I'm fully on board with your test driven approach. Locating an issue is only made easier by limiting the running code to bare essentials.


#8

Oh yeah, tabs are evil. (Highly opinionated, not fact)

Just don't mix them (and 4 spaces is preferred according to PEP 8)


#9

I think this warrants a suggestion for the platform people: write 4 spaces into the auto-indent and disable or strip tabs.

Add.

A long time ago I did this with my text editor by setting tab to 0 spaces. Don't know if they let you do that any more. Haven't tried, but now's a good chance. Notepad++ away.

Add

Well, that let me disable auto-indent, is all. Couldn't find the tab size but will keep looking.

We do need this level of trust in the course IDE's, and if tabs aren't accepted, then learners learn 4 spaces.


#10

Hi guys, I was just wondering why the code doesn't work when you put the trip prices in quotation marks?


#11

If all the prices are strings, then they will have to be converted to integers to be added for a total. That would take extra code, and explaining, perhaps for some. Easier this way to work with number primitives and avoid any hassle.


#12

Oh, that's why. Thank you for the explanation.


#14

N boom.. yours was just a spelling error.. Pittsburgh and you had Pittsburg


#16