Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. # Primary Requirements
  2. # Create an election program that accepts input from the user and outputs the winner of the election
  3.  
  4. # Input: Poll 10 people for their election vote
  5. # Output: Print the total votes cast as well as the winner of the election
  6.  
  7. #identify candidates and hash for vote count
  8. all_candidates = ["Kaya", "Gus", "Gilbert", "Wolfy", "Snickers"]
  9. voting_box = {}
  10. total_votes = 10
  11.  
  12. #welcome
  13. puts "This is the Election Voting Program. Are you ready to vote?"
  14. puts "Here are the candidates for this upcoming term:"
  15.  
  16. #print list of candidates
  17. for i in 0..all_candidates.length
  18. puts all_candidates[i]
  19. end
  20.  
  21. puts "Time to cast your vote!"
  22.  
  23. # ask for votes up to 10 votes
  24. for ballet in 1..total_votes
  25. print "Vote #" + ballet.to_s + ":"
  26. candidate = gets.chomp.capitalize
  27.  
  28. #check if ballet is blank or incorrect candidate
  29. while all_candidates.include?(candidate) === false
  30. print "Your ballet is either empty or you have the wrong candidate. Re-vote: "
  31. candidate = gets.chomp.capitalize
  32. end
  33.  
  34. #check if candidate is a key in voting_box
  35. if voting_box.key?(candidate)
  36. #add 1 vote if so
  37. voting_box[candidate] +=1
  38. else
  39. #push new candidate into hash with 1 as vote_count
  40. voting_box[candidate] = 1
  41. end
  42. end
  43. puts
  44.  
  45. #display hash
  46. puts "ELECTION RESULTS..."
  47. puts "Vote summary:"
  48. voting_box.each do |key, value|
  49. puts "#{key}: #{value} vote(s)"
  50. end
  51. puts
  52.  
  53. #print winner, the key with the largest value
  54. puts "WINNER IS: " + voting_box.key(voting_box.values.max).upcase + "!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement