Saturday, August 8, 2015

Counting the Votes Using Python

In this blog post we will see how to read a text file and perform some analysis like counting the votes.
Find the attached text  to perform the analysis  and save it as a text file.
http://opentechschool.github.io/python-data-intro/files/radishsurvey.txt 

Now after saving the file as .txt file we have to load the file into the python environment
so in order to load the file go to the python directories in your system and then paste the text file there .

 The primary objectives are

  • What's the most popular radish variety?
  • What are the least popular?
  • Did anyone vote twice?
Here the data consists of two parts one is the name and the other is to which they voted
 Here we will used functions split() and strip() -- as our raw data is clumsy we will split the data line wise and who are the person's voted for Icicle and the count for Icicle

















Now I would like to print the names of customers who only voted for a particular radish variety and lets make it little arranged. Lets take it as White Icicle.
The now I want to see how many voted for the White Icicle so we used a loop condition by using loop condition we can count how many voted
so the total members voted are 59 (highlighted in red)

Now I have to answer my objectives

who is the most and least popular
and who voted more than once ????

but here the main problem is capital letter for example
Red King
red king

Both the words are similar to us but they are different for python so in order to remove it we will use
vote = vote.strip().capitalize()
  which will remove the capital letters and make both the words 
as same to python
The other problem is double spaces in order to remove the double spaces we will use
vote = vote.replace("  ", " ")
  which will help us to remove the double spaces and keep single space
 instead of double 



So from the above we got to know that the loser is red king and plum purple with 56 votes and there are 2 people who voted twice phobe barwell and procopio zito are the two who voted twice
The other question left out is who got the maximum  number of votes
So lets go and print the WINNER.


  so the winner is champion with 76 votes

No comments:

Post a Comment