I'm trying to get a population density formula to work, where the user inputs population, area, and square unit of area (km vs mi). Error is printed when user either inputs in negative numbers for population or area, or inputs units that are not "mi" or "km". Both errors print unique error messages (see code below), but they also pass an output parameter of "-1".
If neither errors happen, I want to pass the density value as an output parameter.
But I can't figure out how to pass a return statement in a function more than once, since the function terminates after the first return.
def pop_density(pop,land_area,distance_unit):
density = float(pop)/land_area
if not distance_unit = "mi" or distance_unit = "km":
print "ERROR (Invalid Units)"
return "-1"
if pop < 0 or land_area < 0:
print "ERROR (Invalid Number)"
return "-1"
return density
Aucun commentaire:
Enregistrer un commentaire