Мне нужно ввести несколько городов и их население, а также вывести максимальное и минимальное население с названием города.
Я понял, как получить максимум и распечатать его вместе со своей страной, но я не знаю, как получить минимум.
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()!
О, если вы используете Python 3, я думаю, что это max = sys.maxsize
и min = -sys.maxsize
.
Использовать этот:
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
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
Возможно, вы захотите сначала инициализировать свои значения (т.е. перед циклом). Например.
min = sysmaxint
иmax = - (sys.maxint - 1)
или аналогичные. В основном вам нужно установить начальные значения в крайние значения, чтобы они соответственно запускали ваши операторы if. В качестве альтернативы вы можете инициализировать их первым значением в вашем списке.