简体   繁体   中英

Learning python on codecademy, i'm not sure how I got this wrong

I'm learning py on codecademy, and I got stuck on one of the questions. Here's the prompt:

Below your existing code, define a function called rental_car_cost with an argument called days.

Calculate the cost of renting the car:

Every day you rent the car costs $40. if you rent the car for 7 or more days, you get $50 off your total. Alternatively (elif), if you rent the car for 3 or more days, you get $20 off your total. You cannot get both of the above discounts. Return that cost.

Just like in the example above, this check becomes simpler if you make the 7-day check an if statement and the 3-day check an elif statement.

Here's my code:

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

It's rejecting my code, saying it can't find rental_car_cost. What did I do wrong?

It seems there is an extra space in front on your function definition:

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

and it should be

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

Python is strict about indentation...

When I run the below code, I get no errors and I get the return of '100'.

Are you sure you made no typos, unintended indentation and that you called the method correctly with an integer as parameter?

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

def main():
    print(rental_car_cost(3))


if __name__ == '__main__':
    main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM