[Tutor] if statement issue
Alan Gauld
alan.gauld at yahoo.co.uk
Sat Apr 25 10:32:22 EDT 2020
On 25/04/2020 08:25, shubham sinha wrote:
> question:
> The City class has the following attributes: name, country (where the city
> is located), elevation (measured in meters), and population (approximate,
> according to recent statistics).
That's a terrible class definition. But assuming this is a homework and
you must use it... Attributes of a class are usually set in its init()
method. You should be able to instantiate a city with something like:
london = City('London', 'UK', 30, 9000000)
> max_elevation_city function to return the name of the city and its country
> (separated by a comma), when comparing the 3 defined instances for a
> specified minimal population. For example, calling the function for a
> minimum population of 1 million: max_elevation_city(1000000) should return
> "Sofia, Bulgaria".
This is the more interesting part.
You effectively need to filter by population and sort by elevation.
You can do it either way around but I'd suggest filtering first.
> # define a basic city class
> class City:
> name = ""
> country = ""
> elevation = 0
> population = 0
You need an init method.
And you need to observe proper indentation. Python is very
fussy about indentation.
> # 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
You really don't want to do that.
Create an __init__() method in City.
> def max_elevation_city(min_population):
> # Initialize the variable that will hold
> # the information of the city with
> # the highest elevation
> return_city = City()
You don;t really need this since you already have the city objects and
you can just return one of them. But if you must do it this way you art
least need to initialize the elevation value.
> # 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 >
> return_city.elevation:
You are comparing to return_city.elevation but you never set it anywhere
(Because you never created an init() method.
> 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 >
> return_city.elevation:
> return_city = city2
This is duplicate code. You code remove the duplication by using a for loop:
for city in [city1,city2,city3]:
if city....
return_city = city
> #Format the return string
> if return_city.name:
> return ("{}, {}".format(return_city.name, return_city.country))
> else:
> return ""
> In the "#Format the return string" why we did not call for True statement
> for if statement and it also gives the correct answer?
I assume you mean why did you have
if return_city.name:
instead of
if return_city.name != "":
Or similar?
The answer is that Python treats any value that is zero or empty
as False and anything else is True. So in the if statement
we effectively check whether the name is not empty.
I'm not sure how many of my comments above are down to you
and how much is down to the homework template. But it always
saddens me when I see students being given bad code as a
template to start from!
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list