Oops, try again. rental_car_cost(1) returned 20 instead of the correct value 40


#1

As far as I can tell, my math works just fine but don't know why it's wants 40?

def rental_car_cost(days):
    price = 40 * days
    if nights >= 7:
        price -= 50
    elif nights >= 3 and nights <= 6:
        price -= 20
    return price

I put in 5 days. Which means 140*5, then subtract 20 for being more than 3(but less than 7) which works out to 180. What is the error message meaning then?


#2

@codeace61552,

With
def rental_car_cost(days):
you define a rental_car_cost function which takes 1 parameter days
This days parameter
is used as a variable throughout the rental_car_cost function

If you call the rental_car_cost function
rental_car_cost()
and the rental_car_cost function was defined as having 1 parameter
you will have to provide 1 argument being a number Value
in our case that would be 1
like
rental_car_cost(1)

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

now the real question

Where is the nights variable coming from ??


#3

The nights variable is coming from a raw_input later down in the code and ultimate is irrelevant afaik. The whole exercise gets rather confusing when they are using nights and days that ultimately mean the same number but here's the code in it's entirety.

def hotel_cost(days):
    return (140*days)
    
def plane_ride_cost(city):
    if city == "Charlotte":
        return 183
    elif city == "tampa":
        return 220
    elif city == "pittsburgh":
        return 222
    elif city == "los Aageles":
        return 475

def rental_car_cost(days):
    price = 40 * days
    if nights >= 7:
        price -= 50
    elif nights >= 3 and nights <= 6:
        price -= 20
    return price
    
def trip_cost(city, days):
    return hotel_cost(nights) + plane_ride_cost(city) +rental_car_cost(nights)
    
nights = int(raw_input("How many nights? "))
print hotel_cost(nights)
city = raw_input("What city? ")
print plane_ride_cost(city)
print rental_car_cost(nights)
print trip_cost(city, nights)

I've added some extra print statements to check things as they go along. They all work out as expected except I still do not understand the error message.

This is just taking forever to sink in and make sense.


#4

Try it with

def rental_car_cost(days):
    price = 40 * days
    if days >= 7:
        price -= 50
    elif days >= 3 and nights <= 6:
        price -= 20
    return price

=

With
def rental_car_cost(days):
you define a rental_car_cost function which takes 1 parameter days
This days parameter 
is used as a variable throughout the rental_car_cost function

If you call the rental_car_cost function
rental_car_cost()
and the rental_car_cost function was defined as having 1 parameter
you will have to provide 1 argument being a number Value
in our case that would be 1
like
rental_car_cost(1)

some quotes from the outer-world:

**argument is the value/variable/reference being passed in, 
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
 while "arguments" are called "actual parameters".**

now the real question

Where was the nights variable coming from ??

#5

Hi @leonhard_wettengmx_n

That seems to make it worse actually:

"Oops, try again. plane_ride_cost('Tampa') returned None instead of the correct value 220"

I'd rather try to understand why the original error message was giving me some numbers that didn't seem to match up to anything.

Hmm, let's look at where that number might be coming from?

"
Oops, try again. rental_car_cost(1) returned 20 instead of the correct value 40"

The only thing I can see is that that '20' could only cmoe from the original price of "40 minus the 20 from the length of stay discount of 5 nights that I choose. Let's see what happens when I choose a different length of stay.

When I choose 1 night:

"Oops, try again. rental_car_cost(3) returned 120 instead of the correct value 100"

So the math is not working right, despite the previous exercise letting it pass just fine.


#6

ok, so I tried another approach.

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

Got teh same damn error message! 'Oops, try again. rental_car_cost(1) returned 20 instead of the correct value 40"


#7

@codeace61552,
Maybe,

++++ reset of session *****************
under certain circumstances you can shoot your Browser in an
inconsistent state.

Therefor it is of an advantage to know that you have 2 reset facilities:

One is the use of the F5-key which does a refresh Browser

and

Two, select&copy your code
Then use the Reset Code button of the course-window,
then paste your code back in.

Addendum
General Notes:
Always refresh the browser after making corrections:
CTRL f5 ( if on Windows or Linux)
CMD r ( if on a MAC).
CTRL 0 to reset browser zoom


#8

Been doing that a lot actually. Occasionally I have to hit CTL-F5 to force refresh everything. I'll try the reset code part though.

This is the thing that bothers me most about online learning environments, they have too many technical variations that create further complications.

BTW, long time tech, short time python learner :smile:


#9

Yeah...that didn't change anything. >:expressionless:


#10

So, I'm seeing some sort of pattern emerging.

When I use 5 for the number of nights, I get rental_car_cost(1) returned 20....

When I use 1 night, I get rental_car_cost(3) returns 120... 120 is the cost of 40 * 3. why is my '1' somehow becoming a 3??

So I tried yet another approach. Screw this nights/days thing. I changed everything to "days" and I got:

"Oops, try again. plane_ride_cost('Tampa') returned None instead of the correct value 220"

This is leading me more and more to suspect an environment issue. No where do I have it spelled "Tampa". I purposed changed it to lowercase 'tampa' to make input quicker.

*Correction, when I first wrote the code, I did use "Tampa" but changed it a couple of days ago. When I reset it today, it reverted to "Tampa" but I pasted my updated coded with "tampa" in it and it's still seeing "Tampa"

How annoying.


#11

@codeace61552,
You are still using nights within the FUNCTION-BODY,
only days is a valid variable-name !!!!!!!!!

def rental_car_cost(days):
    price = 40 * days
    if nights &gt;= 7:
        price -= 50
    elif nights &gt;= 3 and nights &lt;= 6:
        price -= 20
    return price

#12

@codeace61552,
could you give me the exact exercise-name, and section-number you are in,
as you are trying this ????

I got a pass with your code (after changing nights into days) in section 4 Transportation....


#13

My Bad,forgot to update that part here. I did switch and now my current code doesn't like my answer to "tampa"

"Oops, try again. plane_ride_cost('Tampa') returned None instead of the correct value 220"

def hotel_cost(days):
    return (140*days)
    
def plane_ride_cost(city):
    if city == "Charlotte":
        return 183
    elif city == "tampa":
        return 220
    elif city == "pittsburgh":
        return 222
    elif city == "los Aageles":
        return 475

def rental_car_cost(days):
       if days >= 7:
           return days*40 - 50
       elif days >= 3 and days < 7:
           return days*40 - 20
       else:
           return days * 40
    
def trip_cost(city, days):
    return hotel_cost(days) + plane_ride_cost(city) +rental_car_cost(days)

days = int(raw_input("How many nights? "))
print hotel_cost(days)
print rental_car_cost(days)
city = raw_input("What city? ")
print trip_cost(city, days)

This is getting ridiculous. I've spent nearly all most time trying to figure this out.


#14

Err, we're posting in the exact exercise forums actually :smiley: It's right at the top of your screen.


#15

1

I only have 7 sections....

2

you really want to fly to

"los Aageles"

3

could you give me the instructions
of the section you are in....


#16

As expected, this is an environment issue.

I changed my 'tampa' to "Tampa", tried again and got a whole new error message:

Oops, try again. plane_ride_cost('Pittsburgh') returned None instead of the correct value 222"

which is rather confusing because I choose Tampa. SMH. (Not to mention having top Ctl-F5 nearly every single time I change my code or else the site just freezes when the spinning gear on the submit button)

So I changed all my cities with proper capital letters and guess what...it works?!?!

As I expected from the begging, this is an environment issue which has lead to a ton of wasted hours on something that had utterly nothing to do with the lesson.


#17

@codeace61552,
But it had something to do with exactly following the specification's...


#18

Yeah...I knew you'd say that. And no it didn't. There is quite a difference between practicing and producing. Those cities could have been any cities. The vast majority of professional coders would know to use the right terms. For practicing, this is purely ludicrous. I followed exact and proper code. The cities used should not have caused such issues. The issues raised gave error messages that were utterly misleading. Thus an environment issue which served only to confuse end results.

If this was some client's program that was going to be sold or live online, this obviously proper name would have been used but to have such errors in a PRACTICE setup shows a lack of flexibility in the tool/organizer. Not a damn thing to do with specifications.


#19

@codeace61552,
The specifications

"Charlotte": 183
"Tampa": 220
"Pittsburgh": 222
"Los Angeles": 475

#20

OK, thanks for the help, I honestly do appreciate it.