Я должен указать несколько городов и их население, а также указать максимальное и минимальное население с названием города.

Я понял, как получить максимум и распечатать его вместе со своей страной, однако, я не знаю, как получить минимум.

n =int(input("Enter the number of municipalities "))

for i in range(n):

    city = input("Enter the municipality: ")
    population = float(input("Enter the population: "))
    #processing information to calculate sum and max, min and range
    sum += population

    if max < population:
        max = population
        maxcity = city

print("The municipality with the highest population of", max, "million was " + maxcity)

Для мин мне нужен тот же результат, что и для макс. Что я могу добавить в цикл if, чтобы это произошло? Мне запрещено использовать встроенные функции max (), min ()!

0
Nima Pishva 29 Май 2019 в 10:36

2 ответа

Лучший ответ

Использовать это:

max_value = 0
max_city = ''
min_value = 10**20
min_city = ''
for i in range(n):

    city = input("Enter the municipality: ")
    population = float(input("Enter the population: "))

    if max_value < population:
        max_value = population
        max_city = city

    if min_value > population:
        min_value = population
        min_city = city

print("The municipality with the highest population of", max_value, "million was " + max_city)
print("The municipality with the lowest population of", min_value, "million was " + min_city)

Выход:

Enter the municipality: f
Enter the population: 10
Enter the municipality: g
Enter the population: 15
Enter the municipality: h
Enter the population: 80

The municipality with the highest population of 80.0 million was h
The municipality with the lowest population of 10.0 million was f
1
Zaraki Kenpachi 29 Май 2019 в 07:57
import sys

INT_max_population = sys.maxsize  
INT_MIN = -sys.maxsize-1

max_city = ''
min_city =''

max_population = INT_MIN
min_population = INT_max_population

n =int(input("Enter the number of municipalities "))

for i in range(n):
    city = input("Enter the municipality: ")
    population = float(input("Enter the population: "))

    if max_population < population:
        max_population = population
        max_city = city

    if min_population>population:
        min_population = population
        min_city = city

print("The municipality with the highest population of {} million was {} ".format(max_population, max_city))
print("The municipality with the lowest population of {}  million was {} ".format(min_population, min_city))        

Выход

Enter the number of municipalities 3

Enter the municipality: a

Enter the population: 1

Enter the municipality: b

Enter the population: 2

Enter the municipality: c

Enter the population: 3
The municipality with the highest population of 3.0 million was c 
The municipality with the lowest population of 1.0  million was a 
0
sahasrara62 29 Май 2019 в 07:55