Python- Taking a Vacation


#1
> def rental_car_cost(days):
>       if days>=7:
>             return (40*days-50)
>       elif days>=3:
>             return (40*days-20)

I don’t understand the problem with this code, it keeps on giving me an indentation error.


#2

Hi grim11

Python is, unlike many (if not all) other programming languages, ruthless when it comes to making sure you’ve indented your code.

If you’ve not indented the code in the way that Python expects you to, you’ll get an indentation error.

Try running it again, but with a 2-space indentation inside your definition and if statement, like this:

def my_function(arg):
  if test:
    do_stuff()
  else:
    do_other_stuff()

(Edited to correct a misleading statement, as per my subsequent reply later in the thread.)


#3

Yes, I got it to work now. Thanks!


#4

Be sure to return the undiscounted amount…

  else:
    return days * 40

#5

Will keep that in mind. Thanks!


#6

You’re welcome. Something else I meant to point out… Python return values do not need parenthesis.

return days * 40 - 50
return days * 40 - 20

#7

No worries, I’m glad you got your code working.

That being said, I need to revise my previous post slightly as I may have misled you.

I said “you may have over-indented the code”, and whilst the Codecademy editor defaults to indenting by 2 spaces this is not actually a requirement of Python if I remember correctly.

To the best of my knowledge, Python doesn’t mind how many spaces you use to indent so long as you’re consistent.

With that in mind, I looked back over your original code snippet and ran it myself without any errors so I’m not entirely sure why you got the error you did. It’s possible that there was something about the indentation which wasn’t preserved when you posted to the forum, perhaps if you used tab to indent rather than space this might have been misinterpreted by the editor, but either way I’m confused as to why that didn’t work for you.

Quirk of the editor, maybe?


#8

That’s exactly what I thought too, since I usually use tabs instead of spaces. The second time, when I tried using spaces, the same exact code started working for me. Could be a possible glitch


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.