Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- reading survey #openintro
- @author: amr
- """
- # Create an empty dictionary for associating radish names
- # with vote counts
- from pprint import pprint
- counts = {}
- voted = []
- for line in open("radishsurvey.txt"):
- line1 = line.strip()
- #print line
- name, vote = line1.split(" - ")
- vote = vote.strip().capitalize()
- vote = vote.replace(" "," ")
- if name in voted:
- print name, "has already voted"
- continue
- voted.append(name)
- if vote not in counts:
- # First vote for this variety
- counts[vote] = 1
- else:
- # Increment the vote count
- counts[vote] = counts[vote] + 1
- """
- for name in counts:
- print name, counts[name]
- """
- def find_winner(counts):
- winner = ""
- pre_vote = 0
- for vote in counts:
- if counts[vote] >= pre_vote:
- winner = vote
- pre_vote = counts[vote]
- return winner, pre_vote
- def find_loser(counts):
- loser, pre_vote = find_winner(counts) #calling a function inside another fn.
- for vote in counts:
- if counts[vote] < pre_vote:
- loser = vote
- pre_vote = counts[vote]
- return loser, pre_vote
- #pprint(counts)
- for item in counts:
- print item, counts[item]
- winner,votes = find_winner(counts)
- print "And the winner is Mr. %s with %d votes" %(winner,votes)
- loser,votes = find_loser(counts)
- print "Sorry, the loser is Mr. %s with %d votes" %(loser,votes)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement