{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "##########\n",
    "# PyPoll #\n",
    "##########\n",
    "\n",
    "# You will be given a set of poll data called election_data.csv. The dataset is composed of three columns:\n",
    "# Voter ID, County, and Candidate. Your task is to create a Python script that analyzes the votes and calculates\n",
    "# each of the following:\n",
    "# The total number of votes cast\n",
    "# A complete list of candidates who received votes\n",
    "# The percentage of votes each candidate won\n",
    "# The total number of votes each candidate won\n",
    "# The winner of the election based on popular vote.\n",
    "\n",
    "# --------------------\n",
    "# Initialize variables\n",
    "# --------------------\n",
    "total_votes = 0\n",
    "candidates_list = []\n",
    "\n",
    "# ---------------------------------------\n",
    "# Get set up to Read in election_data.csv\n",
    "# ---------------------------------------\n",
    "# C:\\# Gregs Temp\\Rice U DABC\\#5 - Rice U DABC - Homework\\Rice U DABC Homework #03 - Python\\election_data.csv\n",
    "import os\n",
    "import csv\n",
    "csvpath = os.path.join('C:', '/', '# Gregs Temp', 'Rice U DABC', '#5 - Rice U DABC - Homework', 'Rice U DABC Homework #03 - Python',\n",
    "                       'election_data.csv')\n",
    "\n",
    "# --------------------------\n",
    "# Read in & Process the file\n",
    "# --------------------------\n",
    "with open(csvpath, 'r', newline='') as csvfile:\n",
    "    csvreader = csv.reader(csvfile, delimiter=',')\n",
    "\n",
    "    # Read the header row first to position ourselves for processing the data\n",
    "    # This file does have a header row\n",
    "    csv_header = next(csvreader)\n",
    "    \n",
    "    # Read each row of data after the header\n",
    "    for row in csvreader:\n",
    "        \n",
    "        \n",
    "     \n",
    "    # Increment the month counter; each row is a new month\n",
    "        month_count = (month_count + 1)\n",
    "        # Collect the profit & loss from the 2nd column in the row\n",
    "        total_pl = (total_pl + int(row[1]))\n",
    "        # Collect the greatest-increase-in-profits\n",
    "        if int(row[1]) > int(greatest_increase_in_profits):\n",
    "            # Save that amount from the 2nd column\n",
    "            greatest_increase_in_profits = row[1]\n",
    "            # Save the date of the greatest-increase-in-profits from the 1st column\n",
    "            giip_date = row[0]\n",
    "        # Collect the greatest-decrease-in-profits\n",
    "        if int(row[1]) < int(greatest_decrease_in_profits):\n",
    "            # Save that amount from the 2nd column\n",
    "            greatest_decrease_in_profits = row[1]\n",
    "            # Save the date of the greatest-decrease-in-profits from the 1st column\n",
    "            gdip_date = row[0]\n",
    "\n",
    "# ------------------------------------------------------------\n",
    "# We're all done processing rows from the csv file\n",
    "# Calculate the percentage of votes for each candicate\n",
    "# ------------------------------------------------------------\n",
    "average_pl = (total_pl / month_count)\n",
    "\n",
    "# ------------------------------------------------------------------------------\n",
    "# Since we're writing the Election Results to a text file as well as printing it\n",
    "# to the terminal, prepare the Election Results report lines first\n",
    "# ------------------------------------------------------------------------------\n",
    "line1 = \"Election Results\"\n",
    "line2 = \"--------------------------------------------\"\n",
    "line3 = (\"Total Votes: \" + str(total_votes))\n",
    "line4 = \"--------------------------------------------\"         \n",
    "\n",
    "line5 = \"--------------------------------------------\"\n",
    "line6 = (\"Winner: \" + winners_name)\n",
    "line7 = \"--------------------------------------------\"\n",
    "\n",
    "# ------------------------------------------------\n",
    "# Write the Election Results report to a text file\n",
    "# ------------------------------------------------\n",
    "# Store the file path associated with the file (note the backslash may be OS specific)\n",
    "filepath = os.path.join('C:', '/', '# Gregs Temp', 'Rice U DABC', '#5 - Rice U DABC - Homework',\n",
    "                        'Rice U DABC Homework #03 - Python', 'election_results.txt')\n",
    "\n",
    "# Open the file in \"write\" mode ('w') and store the contents in the variable \"text\"\n",
    "#with open(filepath, 'w') as text:\n",
    "\n",
    "    # Store all of the text inside a variable called \"lines\"\n",
    "#    lines = text.write()\n",
    "#    text.write() = str(line) + str(iline)\n",
    "    # Print the contents of the text file\n",
    "#    print(lines)\n",
    "\n",
    "# -------------------------------------------------\n",
    "# Print the Election Results report to the terminal\n",
    "# -------------------------------------------------\n",
    "#For i in Range(7):\n",
    "#    print(line + (i + 1))\n",
    "\n",
    "#For i in range(7):\n",
    "#    print(i)\n",
    "\n",
    "print(line1)\n",
    "print(line2)\n",
    "print(line3)\n",
    "print(line4)\n",
    "print(line5)\n",
    "print(line6)\n",
    "print(line7)\n",
    "\n",
    "# End of PyPoll program\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
