Skip to content

Instantly share code, notes, and snippets.

@ChantalDemissie
Created January 21, 2019 08:12
Show Gist options
  • Save ChantalDemissie/9f8887802a62de454981ce23db4113c0 to your computer and use it in GitHub Desktop.
Save ChantalDemissie/9f8887802a62de454981ce23db4113c0 to your computer and use it in GitHub Desktop.
Election program assignment
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!"
@ston1x
Copy link

ston1x commented Jan 21, 2019

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

writeins = {
  "dogs" => 0,
  "cats" => 0,
  "rodents" => 0,
}

It is a common practice to write:

writeins = {
  dogs: 0,
  cats: 0,
  rodents: 0,
}

You can address them as writeins[:cats]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment