Home > Enterprise >  I am not getting the wanted result in a problem related to OOP, is it about the conditionals?
I am not getting the wanted result in a problem related to OOP, is it about the conditionals?

Time:05-28

I have been learning Python from Google Crash Course. There is a question in a quiz related to OOP. I wrote the code but can't get the wanted result. I am not sure if I can use 3 if statements like in the code because I have usually seen people use conditionals in if,elif,else order.

    # define a basic city class
class City:
    name = ""
    country = ""
    elevation = 0 
    population = 0

# create a new instance of the City class and
# define each attribute
city1 = City()
city1.name = "Cusco"
city1.country = "Peru"
city1.elevation = 3399
city1.population = 358052

# create a new instance of the City class and
# define each attribute
city2 = City()
city2.name = "Sofia"
city2.country = "Bulgaria"
city2.elevation = 2290
city2.population = 1241675

# create a new instance of the City class and
# define each attribute
city3 = City()
city3.name = "Seoul"
city3.country = "South Korea"
city3.elevation = 38
city3.population = 9733509


def max_elevation_city(min_population):
    # Initialize the variable that will hold 
# the information of the city with 
# the highest elevation 
    return_city = City()

    # Evaluate the 1st instance to meet the requirements:
    # does city #1 have at least min_population and
    # is its elevation the highest evaluated so far?
    if city1.population >= min_population and (city1.elevation > city2.elevation and city1.elevation> city3.elevation):
        return_city = city1
    # Evaluate the 2nd instance to meet the requirements:
    # does city #2 have at least min_population and
    # is its elevation the highest evaluated so far?
    if city2.population>= min_population and (city2.elevation >city1.elevation and city2.elevation>city3.elevation):
        return_city = city2
    # Evaluate the 3rd instance to meet the requirements:
    # does city #3 have at least min_population and
    # is its elevation the highest evaluated so far?
    if city3.population>=min_population and(city3.elevation>city1.elevation and city3.elevation>city2.elevation):
        return_city = city3

    #Format the return string
    if return_city.name:
        return return_city.name, return_city.country
    else:
        return ""

print(max_elevation_city(100000)) # Should print "Cusco, Peru"
print(max_elevation_city(1000000)) # Should print "Sofia, Bulgaria"
print(max_elevation_city(10000000)) # Should print 

Here is the result that I get:

('Cusco', 'Peru')



CodePudding user response:

Based on your code it looks like you are trying to get the city that has at least the minimum population and the highest elevation of those that meet the minimum population. If so the code is running into issues based on the if statements. Because city2 and city3 have lower elevation then city1 the last two if statements will never be true. This can be seen by tracing your code.
the first if statement for the second call looks like this
358052 > 1000000 and (3399 > 2290 and 3399 > 38) => False and (True and True) => False
The second if statement looks like this
1241675 > 1000000 and (2290 > 3399 and 2290 > 38) => True and (False and True) => False
Based on logic this means that city two Sofia will never be a return value with these parameters. to get the desired output you must first filter out cities that do not have the minimum population and then find which has the highest elevation of those. to simplify this we can use some built in methods. the code should look like this

def max_elevation_city(min_population):
    cities = [city1,city2,city3]
    cities_with_at_least_min_pop = list(filter(lambda city: city.population >= 
       min_population,cities))
    if len(cities_with_at_least_min_pop) == 0:
        return ''
    max_elveation = max(cities_with_at_least_min_pop,key=lambda x: x.elevation)
    return max_elveation.name, max_elveation.country

Breaking this down into understandable pieces.
The filter method takes in two arguments. the first is a function that takes in a city and return a bool. in this case it takes in a city and return true if population is greater then min otherwise false. the second argument of filter is an array of cities to filter. this function then returns an iterable of cities that returned true from the function. we wrap this in list() to make the result into a list.
next we call max, this takes in a list and returns the item in the list with the highest value based on key. key is a function that takes in a city and returns the part of the city used to compare for max. in this case that is elevation. the returned value is the city with the max population.

CodePudding user response:

when you called the function for the second time with print(max_elevation_city(1000000))

-the first if statement is false : because City1 population is less than min_population

-the second if statement is also false!!!! : because (city2.elevation >city1.elevation) is false

the problem with the code is that you cannot compare the evaluation of one city that satisfy the min population condition and the other dose not satisfy this condition

def max_elevation_city(min_population):
    #create an empty list that will contain the cities that has more than the min_population
    cities = []
    if city1.population > min_population:
        #add the city to the list
        cities.append(city1)

    if city2.population > min_population:
        cities.append(city2)
    if city3.population > min_population:
        cities.append(city3)

    #find the max evaluation

    max_evaluation = 0
    city_name = ""
    city_country = ""
    # if the list isn't empty
    if cities:
        for c in cities:
            if c.elevation > max_evaluation:
                max_evaluation = c.elevation
                city_name = c.name
                city_country = c.country


    return city_name, city_country
  • Related