#5 Pull it together "Oops, try again. plane_ride_cost('Charlotte') returned None instead of the correct value 183"


#1

I'm knocking my head against a wall here:

def hotel_cost(nights):
cost_nights = nights * 140
return cost_nights

def plane_ride_cost(city):
city = city.lower
if city == 'charlotte':
plane = 183
elif city == 'tampa':
plane = 220
elif city == 'pittsburgh':
plane = 222
elif city == 'los angeles':
plane = 475
else:
print 'You have entered an invalid city name'

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

But the code never even seems to make it to testing out the trip_cost function. WHY?


#2

Where's your print statement for the console to use the trip_cost function ?
you won't get any printout unless you use a print statement for the final analysis of your total costs
Additionally, is this from the part 7 Step ? Because you are missing your spending money addons..
Also, clean up your code like this, useful for further work too

Instead of 
   def hotel_cost(nights):
      cost_nights = nights * 140
      return cost_nights

Use
   def hotel_cost(nights):
       return nights * 140

#3

Ok so I cleaned up that hotel cost code (thanks!) and I added a print statement at the end, not sure if I did it right but I'm still getting the same error message "Oops, try again. plane_ride_cost('Charlotte') returned None instead of the correct value 183"

Here's my code:

def hotel_cost(nights):
return nights * 140

def plane_ride_cost(city):
city = city.lower
if city == 'charlotte':
plane = 183
elif city == 'tampa':
plane = 220
elif city == 'pittsburgh':
plane = 222
elif city == 'los angeles':
plane = 475
else:
print 'You have entered an invalid city name'

def rental_car_cost(days):
cost = 40 * days
if days >= 7:
cost -= 50
elif days >= 3:
cost -=20
return cost
def trip_cost(city, days):
return rental_car_cost(days) + plane_ride_cost(city) + hotel_cost(days)
print trip_cost(tampa, 10)


#4

Oh and I'm not yet to the spending money add on section.


#5

why arn't you running the code with charlotte as the city of choice ?


#6

I get the error "make sure to define a function named trip_cost". Here's my code.

def hotel_cost(nights):
return 140 * nights

def plane_ride_cost(city):
if city == "Charlotte":
return 183

elif city == "Tampa":
    return 220

elif city == "Pittsburgh":
    return 222

elif city == "Los Angeles":
    return 475

def rental_car_cost(days):
cost = 40 * days

if days >= 7:
    cost += -50

elif days >=3:
    cost += -20

return cost

def trip_cost(city, days):
    return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days)

    print trip_cost("Charlotte", 11)

#7

You have to be careful when using Python about how you indent your code.
Now, with the information I have given you, go back and review your code.
It's too easy to give out the answer


#8

It wasn't in the instructions to run it for Charlotte. But I tried it as Charlotte and I got the same error message: "Oops, try again. plane_ride_cost('Charlotte') returned None instead of the correct value 183"

Im confused as to why its a problem now since I didn't get this error when I did the previous exercise where I put in the plane/city values function. Thanks


#9

Your indentation is wonky. Go back to the lessons and brush up on Python indentation. It's hard to gauge the correct indentation from what people put in these forums.
If your last line is out from the left hand margin, you need to correct that and think about why Python would treat that line differently


#10

OK so I brought the last line starting with Print back to the margin. The last section of my code looks like this now:

def trip_cost(city, days):
return plane_ride_cost(city) + hotel_cost(nights) + rental_cost(days)
print trip_cost('Charlotte,' 10)

This still brings an error but it stopped being the last error message I had. Now it says:

File "python", line 26
print trip_cost('Charlotte,' 10)
^
SyntaxError: invalid syntax

The Syntax error on my console seems to point under the 10. Which makes me think that there is something wrong with my 'days' argument in the trip_cost function. But I dont know what it is. I've defined the function using both "days" and "nights" but it still doesn't work. Thanks


#11

Ok, you really need to look at your programming grammer. Just as writing good English is about dotting the I's and crossing the T's, similarly, your coding grammer needs to be perfect or your program wont compile.
So in this case, the python interpreter is giving you the answer of exactly where your error is.
Can you see it ?
You had the syntax right the first time, so how come it changed ?


#12

I'm just dumbfounded here. I've been looking at it for an hour and I have no idea where my code grammar is off. Previously the syntax wasn't right either, all I did was bring the last line so it wasn't indented. Everything I do simply points to my second argument as the problem and I have no idea why.

So for example when I put in this:

def trip_cost(city, days):
return plane_ride_cost(city) + hotel_cost(days) + rental_cost(days)
print trip_cost('charlotte,' 10)

And it says the error is the last line and it's pointing to 10 or whatever number I put in there. Does that mean that the error is right there on the last line, or does it mean that there is a problem with the days argument that is in the trip_cost function?

I really do want to have good coding grammar but I'm not even sure what I'm supposed to be looking for and the answer isn't showing up in my notes or by reviewing this section. Sorry/thanks


#13

The code I typed as follows:

def plane_ride_cost(city):
if city == "Charlotte":
return 183
elif city == "Tampa":
return 220
elif city == "Pittsburgh":
return 222
else :
return 475

def rental_car_cost(days):

if days >= 7 :
 return days*40-50
elif 7 > days >=3:
 return days*40-20
else:
 return days*40

def hotel_cost(days):
return days*140

def trip_cost(city, days):
return rental_car_cost(days)+hotel_cost(days)+plane_ride_cost(city)

Beware of the same categorizes must put together

def trip_cost(city, days):
return rental_car_cost(days)+hotel_cost(days)+plane_ride_cost(city)

The final line days function must put together then take turns to the city function.

If you do like that
def trip_cost(city, days):
return rental_car_cost(days)+plane_ride_cost(city)+hotel_cost(days)

It will show the error I got stuck about one hour just for the order...lol

Try again :grinning:


#14

OK here's the code that's still not working

def hotel_cost(nights):
return nights * 140

def plane_ride_cost(city):
city = city.lower
if city == 'charlotte':
plane = 183
elif city == 'tampa':
plane = 220
elif city == 'pittsburgh':
plane = 222
elif city == 'los angeles':
plane = 475
else:
print 'You have entered an invalid city name'

def rental_car_cost(days):
cost = 40 * days
if days >= 7:
cost -= 50
elif days >= 3:
cost -=20
return cost
def trip_cost(city, days):
return rental_car_cost(days)+hotel_cost(days)+plane_ride_cost(city)
print trip_cost('charlotte,' 10)

When I enter this it still tells me invalid syntax:

File "python", line 26
print trip_cost('charlotte,' 10)
^
SyntaxError: invalid syntax

On the trip_cost function I also tried every combination on the return line for the function of plane trip, hotel and rental. Every one. Ive been stuck on this section for a week and I'm starting to forget all the stuff I learned in previous sections, Im really at a loss here.


#15

Hello I spend approximate 30 mins to inspect your code and modify them. First of all, I think you almost succcess, but some little mistake cause the code didn't work.

Here is the code I typed, and help you to highlight some keypoints.

def hotel_cost(days):
return days * 140

def plane_ride_cost(city):
if city == 'Charlotte':
return 183
elif city == 'Tampa':
return 220
elif city == 'Pittsburgh':
return 222
elif city == 'Los Angeles':
return 475
else:
print 'You have entered an invalid city name'

def rental_car_cost(days):
cost = 40 * days
if days >= 7:
return cost - 50
elif 7>days >= 3:
return cost -20
else:
return cost
def trip_cost(city, days):
return rental_car_cost(days)+hotel_cost(days)+plane_ride_cost(city)
print trip_cost('Charlotte',10)

I summarize some problems that Isee

1.Need to use uppercase to the city
2.Don't forget the key word named return
3.In the return argument don't forget do not use = and something like that
4.Need to put the conditon that you can express the math formula well
eg: A>=7, A>3 <= What will happen when A is 4,5,6 ?? Is A will follow A>=7 or A>3?
It will make code cause error and also it doesn't make sence, so you need to reassign the argument like that: A>=7, 7>=A>3

  1. Finally, when you use if argument ,remember to put else in the final part.

Don't be frustrated just keep going~ :sunglasses:


#16

Thanks, all the points you made were very helpful especially about the elif 7>days>= 3: part. That seems obvious to me now that it doesn't make sense to try and reduce cost for both those if statements....

But, I changed the code like you recommended and I still get the exact same error message, telling me invalid syntax.

Here's what I did:

def hotel_cost(nights):
return nights * 140

def plane_ride_cost(city):
city = city.lower
if city == 'Charlotte':
return 183
elif city == 'Tampa':
return 220
elif city == 'Pittsburgh':
return 222
elif city == 'Los Angeles':
return 475
else:
print 'You have entered an invalid city name'

def rental_car_cost(days):
cost = 40 * days
if days >= 7:
cost -= 50
elif 7>days>= 3:
cost -=20
else:
return cost
def trip_cost(city, days):
return rental_car_cost(days)+hotel_cost(days)+plane_ride_cost(city)
print trip_cost('Charlotte,' 10)


#17

Hello, congratulation you find some ways, but here still have some mistakes that cause your code error, so I will show you step by steps and also highlight them.

  1. You code:

def plane_ride_cost(city):
city = city.lower
if city == 'Charlotte':
return 183
elif city == 'Tampa':
return 220
elif city == 'Pittsburgh':
return 222
elif city == 'Los Angeles':
return 475
else:
print 'You have entered an invalid city name'

The problem is on line 2, because the city.lower you will not use it anymore so I suggest that you can delete it, or can type like city==city.lower it will define the city.lower as a variable( I guess) and will not influence the code execution, rather than the code you typed.However those you typed will cause error, because you use city in the following , not city.lower so the code will make confuse then it will shut down.

2.You code:

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

Again you forgot do not put the = sign in the return argument and need to put return, so the thing must like:

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

  1. Beware of the indent, in "python" it is quite important.

For example, the indent I use in def is 0 (it means do not have any indent)

the return below def has 1 indent(it means need to hit spacebar one time) , like

def hotel_cost(nights): 0 indent
return nights * 140 1 indent

and if you put more and more layer, you need do hit the appropriate time of spacebar.

def plane_ride_cost(city): 0 indent

if city == 'Charlotte': 1 indent
return 183 2 indent

If you make the wrong correspondence then will get error , like

def plane_ride_cost(city): 0 indent

if city == 'Charlotte': 1 indent
return 183 1 indent

File "python", line 7
return 183
^
IndentationError: expected an indented block

Try again you almost near the finish line :hatching_chick:

Best wishes


#18

This indentation problem when users ask for help needs to be fixed up. I wonder how much repetition of the same questions has taken place on all the forums because of this. Really, the website managers need to look into this, its not a very professional way to do things


#19

I can't agree with you any more.

In fact, I think it is a feature of python I use the 3.4 version before and also face the same problem

it really makes me confused and frustrated, until now I reel confused and frustrated too

but I think python is a quite friendly language not like the previous language SAS I learned

ha ha ha ha ha :grinning: :grinning: :grinning: :grinning: :grinning:


#20

So I think I'm confused on what you meant when you typed this in your last reply:

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

The "my code" and "your code" part I dont know what you mean by that? And are you saying there can be no = in a return argument, but what about a -=? Are those OK

I tried this code based on your last advice:

def hotel_cost(nights):
return nights * 140

def plane_ride_cost(city):
if city == 'Charlotte':
return 183
elif city == 'Tampa':
return 220
elif city == 'Pittsburgh':
return 222
elif city == 'Los Angeles':
return 475
else:
print 'You have entered an invalid city name'

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

And I'm still getting the same error message:

File "python", line 26
print trip_cost('Charlotte,' 10)
^
SyntaxError: invalid syntax

I also fixed the indent and hit space instead of tab in the way you described. But I still feel just as confused as ever and the section instructions aren't super helpful. Thanks again!