我在与OOP相关的问题中没有得到想要的结果,是关于条件的吗?



我一直在谷歌速成班学习Python。在一个与OOP相关的测验中有一个问题。我写了代码,但不能得到想要的结果。我不确定我是否可以像代码中那样使用3个if语句,因为我经常看到人们以if,elif,else顺序使用条件。

# 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 

下面是我得到的结果:

('Cusco', 'Peru')

根据您的代码,看起来您正在尝试获得至少具有最小人口的城市和满足最小人口的城市的最高海拔。如果是这样,代码就会遇到基于If语句的问题。因为city2和city3的海拔比city1低,所以最后两个if语句永远不会为真。这可以通过跟踪代码看到。
第二次调用的第一个if语句如下
358052>1000000和(3399>2290和3399>38) =比;False and (True and True) =>False
第二个if语句如下
1241675>1000000和(2290>3399和2290>38) =比;True and (False and True) =>False
根据逻辑,这意味着城市2 Sofia将永远不会是带有这些参数的返回值。为了得到想要的结果,你必须首先过滤掉人口不是最少的城市,然后找出其中海拔最高的城市。为了简化这一点,我们可以使用一些内置方法。代码应该像这样

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

将其分解为可理解的部分。
过滤器方法接受两个参数。第一个是接受城市并返回bool值的函数。在本例中,它接受一个城市,如果人口大于min,则返回true,否则返回false。filter的第二个参数是要过滤的城市数组。然后,该函数返回一个由城市组成的可迭代对象,该对象从函数返回true。我们将其封装在list()中,使结果变成一个列表。
接下来调用max,它接受一个列表并返回列表中键值最大的项。Key是一个函数,它接受一个城市并返回该城市用于比较max的部分。在这种情况下,这是海拔。返回值是人口最多的城市。

第二次使用print(max_elevation_city(1000000))调用函数

-第一个if语句为假:因为City1 population小于min_population

-第二个if语句也是false!!!!因为……海拔>city1.elevation)为false

代码的问题是你不能比较一个满足最小人口条件的城市和另一个不满足这个条件的城市的评估

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

最新更新