Skip to content

Instantly share code, notes, and snippets.

@gibrancastillo
Created July 1, 2021 21:51
Show Gist options
  • Save gibrancastillo/ef3542687868796fef54ba1d5774297c to your computer and use it in GitHub Desktop.
Save gibrancastillo/ef3542687868796fef54ba1d5774297c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "\nEnter the year of interest: 1959\nEnter the country of interest: Germany\n"
},
{
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: 'life-expectancy.csv'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-1-1cafc601fbfd>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 26\u001b[0m \u001b[0mmin_life_expectancy_interest\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m99999999999\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 28\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"life-expectancy.csv\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mcsv_file\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 29\u001b[0m \u001b[0;31m# The file has now been opened and stored in a variable \"csv_file\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'life-expectancy.csv'"
]
}
],
"source": "year_of_interest = int(input(\"\\nEnter the year of interest: \"))\ncountry_of_interest = input(\"Enter the country of interest: \")\ncount = 0\ncount2 = 0\nyear = -1\nmax_year = -1\nmin_year = -1\ncountry = None\nmax_country = None\nmin_country = None\ncountry_code = None\nlife_expectancy = 0\nsum_life_expectancy = 0\nsum_life_expectancy2 = 0\nmax_life_expectancy = 0\nmin_life_expectancy = 99999999999\n\nmax_country_year = None\nmin_country_year = None\nmax_life_expectancy_year = 0\nmin_life_expectancy_year = 99999999999\n\nmax_country_interest = None\nmin_country_interest = None\nmax_life_expectancy_interest = 0\nmin_life_expectancy_interest = 99999999999\n\nwith open(\"life-expectancy.csv\") as csv_file:\n # The file has now been opened and stored in a variable \"csv_file\"\n\n # skip the first line in csv_file, in this case the header\n next(csv_file)\n\n # Read each line, one by one, into a variable: current_line\n for current_line in csv_file:\n # Strip off leading/trailing whitespace, \\t, and/or \\n.\n # This is important, otherwise, our last variable will have \\n characters with it.\n current_line = current_line.strip()\n\n # Split the current line into its parts based on a comma \",\" as the separator\n split_line = current_line.split(\",\")\n\n # Save the parts we need into variables\n country = split_line[0]\n country_code = split_line[1]\n year = int(split_line[2])\n life_expectancy = float(split_line[3])\n\n # Find the lowest value for life expectancy and the highest value for \n # life expectancy in the dataset. \n if life_expectancy > max_life_expectancy:\n max_year = year\n max_country = country\n max_life_expectancy = life_expectancy\n \n if life_expectancy < min_life_expectancy:\n #if life_expectancy_years > 0 and life_expectancy_years < min_life_expectancy_years:\n min_year = year\n min_country = country\n min_life_expectancy = life_expectancy\n\n # Year of interest\n if year_of_interest == year:\n count += 1\n sum_life_expectancy += life_expectancy\n\n if life_expectancy > max_life_expectancy_year:\n max_country_year = country\n max_life_expectancy_year = life_expectancy\n\n if life_expectancy < min_life_expectancy_year:\n min_country_year = country\n min_life_expectancy_year = life_expectancy\n \n if country_of_interest == country:\n count2 += 1\n sum_life_expectancy2 += life_expectancy\n\n if life_expectancy > max_life_expectancy_interest:\n max_country_interest = country\n max_life_expectancy_interest = life_expectancy\n\n if life_expectancy < min_life_expectancy_interest:\n min_country_interest = country\n min_life_expectancy_interest = life_expectancy\n\n\naverage_life_expectancy = sum_life_expectancy / count\naverage_life_expectancy2 = sum_life_expectancy2 / count2\n\n# Output the data as desired\nprint()\nprint(f\"The overall max life expectancy is: {max_life_expectancy:.3f} from {max_country} in {max_year}\")\nprint(f\"The overall min life expectancy is: {min_life_expectancy:.3f} from {min_country} in {min_year}\")\n\nprint(f\"\\nFor the year {year_of_interest}:\")\nprint(f\"The average life expectancy across all countries was {average_life_expectancy:.2f}\")\nprint(f\"The max life expectancy was in {max_country_year} with {max_life_expectancy_year}\")\nprint(f\"The min life expectancy was in {min_country_year} with {min_life_expectancy_year}\")\nprint()\n\nprint(f\"\\nFor the country {country_of_interest}:\")\nprint(f\"The average life expectancy across this country was {average_life_expectancy2:.2f}\")\nprint(f\"The max life expectancy was in {max_country_interest} with {max_life_expectancy_interest}\")\nprint(f\"The min life expectancy was in {min_country_interest} with {min_life_expectancy_interest}\")\nprint()"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": ""
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.7",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment