Advertisement
Guest User

python-assignment

a guest
Aug 9th, 2015
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3.  
  4. reading survey #openintro
  5.  
  6. @author: amr
  7. """
  8. # Create an empty dictionary for associating radish names
  9. # with vote counts
  10. from pprint import pprint
  11. counts = {}
  12. voted = []
  13. for line in open("radishsurvey.txt"):
  14.    
  15.     line1 = line.strip()
  16.     #print line
  17.    
  18.     name, vote = line1.split(" - ")
  19.     vote = vote.strip().capitalize()
  20.     vote = vote.replace("  "," ")
  21.     if name in voted:
  22.         print name, "has already voted"
  23.         continue
  24.     voted.append(name)
  25.     if vote not in counts:
  26.         # First vote for this variety
  27.         counts[vote] = 1
  28.     else:
  29.         # Increment the vote count
  30.         counts[vote] = counts[vote] + 1
  31. """    
  32. for name in counts:
  33.    print name, counts[name]      
  34. """
  35. def find_winner(counts):
  36.     winner = ""
  37.     pre_vote = 0
  38.     for vote in counts:
  39.         if counts[vote] >= pre_vote:
  40.            
  41.             winner = vote
  42.             pre_vote = counts[vote]
  43.            
  44.            
  45.     return winner, pre_vote
  46.    
  47. def find_loser(counts):
  48.     loser, pre_vote = find_winner(counts) #calling a function inside another fn.
  49.     for vote in counts:
  50.         if counts[vote] < pre_vote:
  51.            
  52.             loser = vote
  53.             pre_vote = counts[vote]
  54.            
  55.     return loser, pre_vote
  56.  
  57. #pprint(counts)
  58.                  
  59. for item in counts:
  60.     print item, counts[item]
  61. winner,votes = find_winner(counts)
  62. print "And the winner is Mr. %s with %d votes" %(winner,votes)
  63. loser,votes = find_loser(counts)
  64. print "Sorry, the loser is Mr. %s with %d votes" %(loser,votes)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement