Created
January 21, 2019 08:12
-
-
Save ChantalDemissie/9f8887802a62de454981ce23db4113c0 to your computer and use it in GitHub Desktop.
Election program assignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts "Welcome to the best pet voting program." | |
puts "Pet candidates are: Dogs, Cats, Rodents ." | |
puts "" | |
puts "Which pet has your vote?" | |
puts "" | |
puts "Type in the species or use these shortcut letters for our friends with paws:" | |
puts "For Dogs, type: d" | |
puts "For Cats, type: c" | |
puts "For Rodents, type: r" | |
puts "For writing in your own animal type: o" | |
puts "" | |
writeins = { | |
"dogs" => 0, | |
"cats" => 0, | |
"rodents" => 0, | |
} | |
(1..10).each do |countvote| | |
print "Vote ##{countvote}: " | |
vote = gets.chomp | |
if vote == "d" || vote == "Dogs" | |
writeins["dogs"] += 1 | |
puts "You voted for dogs, woof!" | |
elsif vote == "c" || vote == "Cats" | |
writeins["cats"] += 1 | |
puts "You voted for cats, meow!" | |
elsif vote == "r" || vote == "Rodents" | |
writeins["rodents"] += 1 | |
puts "You voted for rodents, squeak!" | |
elsif vote == "o" || vote == "Other" | |
other_animal = gets.chomp | |
writeins[other_animal] | |
if writeins.include? other_animal | |
writeins[other_animal] += 1 | |
else | |
writeins[other_animal] = 1 | |
end | |
else | |
puts "Not a pet person? Please vote again." | |
end | |
end | |
puts "\n\nElection Results....\n" | |
puts "Vote Summary:" | |
writeins.each do |other_animal, votes| | |
puts "#{other_animal} - #{votes} " + (votes > 1 ? "votes" : "vote") | |
end | |
puts "" | |
print "WINNER: " | |
mostvotes = 0 | |
winner = nil | |
writeins.each do |animal, votes| | |
if votes > mostvotes | |
winner = animal | |
mostvotes = votes | |
end | |
end | |
puts "#{winner} is the winner!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just for your information
Since ruby version 1.9 there is a new syntax that can be used instead of 'hash rockets'. So instead of
It is a common practice to write:
You can address them as
writeins[:cats]