Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Primary Requirements
- # Create an election program that accepts input from the user and outputs the winner of the election
- # Input: Poll 10 people for their election vote
- # Output: Print the total votes cast as well as the winner of the election
- #identify candidates and hash for vote count
- all_candidates = ["Kaya", "Gus", "Gilbert", "Wolfy", "Snickers"]
- voting_box = {}
- total_votes = 10
- #welcome
- puts "This is the Election Voting Program. Are you ready to vote?"
- puts "Here are the candidates for this upcoming term:"
- #print list of candidates
- for i in 0..all_candidates.length
- puts all_candidates[i]
- end
- puts "Time to cast your vote!"
- # ask for votes up to 10 votes
- for ballet in 1..total_votes
- print "Vote #" + ballet.to_s + ":"
- candidate = gets.chomp.capitalize
- #check if ballet is blank or incorrect candidate
- while all_candidates.include?(candidate) === false
- print "Your ballet is either empty or you have the wrong candidate. Re-vote: "
- candidate = gets.chomp.capitalize
- end
- #check if candidate is a key in voting_box
- if voting_box.key?(candidate)
- #add 1 vote if so
- voting_box[candidate] +=1
- else
- #push new candidate into hash with 1 as vote_count
- voting_box[candidate] = 1
- end
- end
- puts
- #display hash
- puts "ELECTION RESULTS..."
- puts "Vote summary:"
- voting_box.each do |key, value|
- puts "#{key}: #{value} vote(s)"
- end
- puts
- #print winner, the key with the largest value
- puts "WINNER IS: " + voting_box.key(voting_box.values.max).upcase + "!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement