Cities & Population
The program must accept the name and population of N cities as the input. The program print the total population in CA the given N cities as the output. Then the program must print the name and population of each city separated by a colon as the output. Please fill in the blanks with code so that the program runs successfully.
Example Input/Output 1:
Input:
3
KKTCity 19000
URCity 81000
MCCity 21000
Output:
121000
KKTCity:19000
URCity:81000
MCCity: 21000
Explanation
Here N=3 and the total population in the given 3 cities is 121000.
Hence the output is
121000
KKTCity:19000
URCity:81000
MCCity:21000
Example Input/Output 2:
Input:
6
MNOCity 25000
BBCity 64250
KPKCity 49999
ABCCity 33750
RRCity 9200
AKKCity 13600
Output:
195799
MNOCity:25000
BBCity:64250
KPKCity:49999
ABCCity:33750
RRCity:9200
AKKCity:13600
Python Program:
class City:
__totalPopulation = 0
def__init__(self, name, population):
self.name = name
self.population population
City._City__totalPopulation+=population
def str (self):
return self.name+":"+str(self.population)
N= int(input())
cities = []
for ctr in range (N):
name, population input().split()
cities.append(City (name, int(population)))
print(City. _City__totalPopulation)
for city in cities:
print(city)
Post a Comment