Transporte


#1

def hotel_cost(nights):
return 140 * nights

def plane_ride_cost(city):
if key == "Charlotte":
return(city[key])
elif key == "Tampa":
return(city[key])
elif key == "Pittsburgh":
return(city[key])
elif key == "Los Angeles":
return(city[key])

lugar = {
"Charlotte" : 183,
"Tampa" : 220,
"Pittsburgh" : 222,
"Los Angeles" : 475
}

for key in lugar:
print str(key + ": " + str(plane_ride_cost(lugar)))

Oops, tente outra vez. Tenha certeza de que plane_ride_cost toma exatamente uma entrada (city).


#2

Seu codigo:

 return 140 * nights

def plane_ride_cost(city):
 if key == "Charlotte":
return(city[key])
 elif key == "Tampa":
 return(city[key])
 elif key == "Pittsburgh":
 return(city[key])
 elif key == "Los Angeles":
 return(city[key])

lugar = {
 "Charlotte" : 183,
 "Tampa" : 220,
 "Pittsburgh" : 222,
 "Los Angeles" : 475
 }

for key in lugar:
 print str(key + ": " + str(plane_ride_cost(lugar)))

Oops, tente outra vez. Tenha certeza de que plane_ride_cost toma exatamente uma entrada (city).`





O que deveria ser:

```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

#3

This approach is okay so long as it can handle Key Errors.

Esta abordagem está correta, desde que ele possa lidar com erros de chave.

def plane_ride_cost(city):
    try:
        return {
          "Charlotte"   : 183,
          "Tampa"       : 220,
          "Pittsburgh"  : 222,
          "Los Angeles" : 475
        }[city]
    except KeyError:
        # print ("%s not in database" % city)
        print ("%s não na base de dados" % city)
        return

print (plane_ride_cost("Tampa"))
print (plane_ride_cost("Miami"))

'''
220
Miami não na base de dados
None
'''