Глядя на операторы печати в следующем формате, также необходимо указать любые новые страны и население, введенные в качестве входных данных. Я могу сделать код для отображения добавленного словаря в формате dict
, но с трудом отображается в следующем формате. Что я делаю неправильно?
Ожидаемый результат:
Vatican has Population 800
Vatican has Population 10200
...
def main():
countryPop = {'Vatican': 800, 'Tuvalu': 10200, 'Nauru': 11000, 'Palau': 17900,
'San Marino': 33420, 'Monaco': 38300, 'Marshall Islands': 55500}
# while loop to repeat the input request and population display until 0 is entered
while True:
ctry = input('Enter country:')
population = countryPop.get(ctry)
print('Population:',population)
if ctry == '0':
break
# Else if Loop meant to activate if a country unknown to the original dictionary is entered
elif ctry not in countryPop:
popIn = input("Country Pop:")
countryPop[ctry] = popIn
# printing the new list after breaking from the loop
for ctry in countryPop:
print(str(ctry)+" has population "+str(popIn))
if __name__ == '__main__':
main()