Transportation error not getting solved


#1
def rental_car_cost(days):
    if days >= 7:
        return 40*7-50
    elif days >=3:
        return 40*3-20
    else:
        return "correct amount(40) for 1 day"

Error is "Oops, try again. It looks like rental_car_cost returns 'correct amount(40) for 1 day' instead of the correct amount (40) for 1 days".


Transportation help
#2

Please format your post as I have done (select entire code block; press Ctrl+Shift+C) from next time. Thanks! :smile:

Read the instructions very carefully.

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

Now, see that you are doing 40*7-50 for the if clause. But, the rate for each day is 40. And you already have the number of days the user has traveled. And, you know that cost = rate * quantity or cost = rate * number. And you have to return cost - discount. Getting my point? Know what error you are making? Can you now fix the error in thisifandelif clause?

Also, note that in the else clause, you only have to return the cost, since discount is zero.


#3

def rental_car_cost(days):

if days >= 7:
    return 40*7-50
elif days >=3:
    return 40*3-20
else:
    return "correct amount(40) for 1 day"

Oops, try again. It looks like rental_car_cost returns 100 instead of the correct amount (140) for 4 days.


#4

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

still getting errror : Oops, try again. It looks like rental_car_cost returns 1 instead of the correct amount (40) for 1 days.

help solve this, I am struck here.


#5

Solved

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

#6

I am happy that my explanation helped you understand the logic :smiley:


#7

thankq @@@@@@@@@@@@@


#8
From the instruction...

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

#9

I was just wondering if there is a reason for returning the cost at every stage of the 'if else' statement. I just solved this with one use of 'return cost'

if days >= 7:
cost -= 50
elif days >= 3:
cost -= 20
return cost

:smile:


#10

But this worked for me after a long while.

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