Skip to content

Instantly share code, notes, and snippets.

@ChaitanyaBaweja
Last active April 2, 2020 12:56
Show Gist options
  • Save ChaitanyaBaweja/d97bdf3f0406ff9b8b3bf7137e8ad666 to your computer and use it in GitHub Desktop.
Save ChaitanyaBaweja/d97bdf3f0406ff9b8b3bf7137e8ad666 to your computer and use it in GitHub Desktop.
from difflib import get_close_matches
population_dict = {'china':1439, 'india':1380, 'usa':331, 'france':65,'germany':83, 'spain':46}
while(True):
Country_Name = input('Please enter Country Name(type exit to close): ').lower()
# break from code if user enters exit
if Country_Name == 'exit':
break
if Country_Name in population_dict.keys():
print(population_dict[Country_Name])
else:
# look for similar strings
maybe_country = get_close_matches(Country_Name, population_dict.keys())
if maybe_country == []: # no similar string
print("Please check for any typos. Data not Available for ",Country_Name)
else:
# user confirmation
ans = input("Do you mean %s? Type y or n."% maybe_country[0])
if ans == 'y':
# if y, return population
print(population_dict[maybe_country[0]])
else:
# if n, start again
print("Bad input. Try again.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment