MatheusRibeiro
MatheusRibeiro

Reputation: 13

Python - TypeError: unsupported operand type(s) for +: 'int' and 'dict'

def hotel(days):
  return days*50

def plane_ticket(city):
  city = {"Chicago": 180, "Boston": 170, "Orlando": 160,"Ciudad de Mexico"    :100}
return city 

def car(days):
  rent=days*10

  if days >= 7:
     return rent -5
  elif days >= 5:
     return rent -1
  else:
     return rent

def trip_cost(city, days, extras):
    return sum([hotel(days),plane_ticket(city),car(days),extras])
print (trip_cost("Chicago",4,300))

I want that the numbers assigned to the strings in the dictionary be used as integers in the sum

Upvotes: 0

Views: 2876

Answers (2)

Andy
Andy

Reputation: 50600

You have this issue because your plane_ticket function isn't returning what you think it is.

def plane_ticket(city):
  city = {"Chicago": 180, "Boston": 170, "Orlando": 160,"Ciudad de Mexico"    :100}
  return city 

This function is returning the entire dictionary city, instead of a single value.

You need to do something like this:

def plane_ticket(city):
  city_prices = {"Chicago": 180, "Boston": 170, "Orlando": 160,"Ciudad de Mexico"    :100}
  return city_prices[city]

You may wish to add some error checking around this though. If the function is passed a city that doesn't exist in your dictionary, you are going to get an error.

Making the change above, prints out the value 720

Upvotes: 3

elzell
elzell

Reputation: 2306

Quite easy:

def plane_ticket(city):
  cities = {"Chicago": 180, "Boston": 170, "Orlando": 160,"Ciudad de Mexico"    :100}
  return cities[city]

Upvotes: 3

Related Questions