5/7 "trip_cost(city,days) raised an error:unsupported operand type(s) for -: 'str' and 'int'"


#1
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):
  if days >= 7:
    return ((days*40) - 50)
  elif days >= 3:
    return ((days*40) - 20)
  elif days < 3 and days >= 0:
    return days*40
  
def trip_cost(days, city):
  return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city)

Why am I getting an error on this?


#2

Which error? Typically an error message contains information about what is wrong. Start there.


#3

The error is trip_cost(‘Charlotte’, 0) raised an error: unsupported operand type(s) for -: ‘str’ and ‘int’, I don’t really know what it’s saying


#4

What the error message says is that you’re doing something similar to:

'blah' - 5  # doesn't make sense to subtract int from str

I suggest reading the error message again to see that this is indeed the case

Also pay attention to how they called your function (the error message tells you)


#5

Interesting. I reloaded it all and the only thing I changed was the order of arguments in the trip_cost argument

Started with def trip_cost(days, city):

which gave an error

And then changed that to def trip_cost(city, days):

And no longer got the error. I also guess I don’t understand what would be wrong with my code either. If you’re adding a value of rental cost cars at a given value of days, with the same days for hotel costs, and then a given value for each city, why was it trying to add a string somewhere in there? Was it trying to add the actual string of the city rather than the return value?


#6

Does your function care about order?

What should div(10, 2) return? what about div(2, 10)

What determines which argument matches which parameter? Because you’ll have to tell them apart somehow, they’re different things. Where is that information?


#7

So with this order of arguments in exercise 7/7 (just adds another argument) I got no error:

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):
  if days >= 7:
    return ((days*40) - 50)
  elif days >= 3:
    return ((days*40) - 20)
  elif days < 3 and days >= 0:
    return days*40
  
def trip_cost(city, days, spending_money):
  return plane_ride_cost(city) + rental_car_cost(days) + hotel_cost(days) + spending_money

print trip_cost("Los Angeles", 5, 600)

which keeps the arguments with city in front of days. This makes sense because rental_car_cost parameter is days, which comes after the parameter for the plane_cost function, city. I get it now why it would need to be that way. And then I believe if used the parameter nights for the hotel_costs function, instead of substituting with days, our order would instead be :

def trip_cost(nights, city, days, spending_money):

Thanks a lot!


#8

I feel like I should really add, that even if you hadn’t come up with that you should try flipping them, what you could/should have done is to copy their call and then have a look at what your function ends up doing. In other words, you’d need to get information out of your function onto your screen, you’d need to print out messages about what you’re doing. If you then check all those steps you’d find that the day and city values were switched somewhere before the function started running.
So when things are wrong somehow, get in there and look.


#9

Just reset exercise and write:

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 = days * 40
  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)

Before reset i get error “unindent does not match any outer indentation level”


#10

That just means your indentation was incorrect, it’s something to fix, not to rewrite!


#11

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