4. Transportation help


#1



Any ideas why this isn't working?


4.0 Transportation


It looks like rental_car_cost returns None instead of the correct amount (40) for 1 days.


moving return to be even with 'def' just gives another error, expected an indented block (python, line 23)


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


#2

In if and elif block, we need to return our rate too ?


#3

hmm if I do this:

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

i receive this error: Oops, try again. It looks like rental_car_cost returns 120 instead of the correct amount (100) for 3 days.


#4

As we give 20 bucks of discount when number must be between 3 or 7(including 3).
we have and ?

You current code says if days are less than 7 then give discount of 20, which is wrong,
we only give discount if days are between 3 and 7.
Like say if days are 2, we cant give discount of 20.

ADDED.

In your last code, there were two issues.
point1 (missed to mention it)
Early introduction of return statement.
As you have introduced return early, when function executes codes inside of it, and sees the return statements,it simply breaks out of it and does not care about code after this.

point2
The second issues discussed at top.

hint for point2.

and you need to change or of elif part.


#5

This is what I did:

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

It seemed to work. My issue with this exercise is that the instructions are incomplete in that they never specify that you are to return the value for a one day rental. Obviously I figured that out from the errors given, but still.


#7

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