我在 codecademy 上的 Python 代码有什么问题?

What is wrong with my Python code on codecademy?

我目前正在 codecademy 上学习 Python,但我被卡住了。这些是方向:

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 . if you rent the car for 7 or more days, you get off your total. Alternatively (elif), if you rent the car for 3 or more days, you get off your total. You cannot get both of the above discounts. Return that cost.

以下是我的代码:

def rental_car_cost(days):
    cost = days * 40
    if days >= 7:
        cost -= 50

    elif days >= 3:
        cost -= 20

    return cost

当我尝试保存并提交该代码时,出现以下错误:

Oops, try again. Did you create a function called rental_car_cost?

有谁知道我做错了什么吗?

函数定义似乎在函数调用下面。 您应该将上面的代码片段放在函数调用之上。
例如下面写的代码会给你一个错误,因为函数在定义之前被调用。

rental_car_cost(7)
def rental_car_cost(days):
  cost = days * 40
  if days >= 7:
    cost -= 50

  elif days >= 3:
    cost -= 20

return cost

下面的代码片段将为我们提供正确的输出。

def rental_car_cost(days):
  cost = days * 40
  if days >= 7:
    cost -= 50

  elif days >= 3:
    cost -= 20

return cost
rental_car_cost(7)