I have a hash with cat names for keys and cat instances for values. Is there a way to make it so that when people type a response that isn’t in a key in the cats hash for the terminal to reprint the puts "Which cat would you like to know about?" question or type: “Try again”? I guess I’m asking for sort of a “while… else”.
puts "Which cat would you like to know about?"
puts cats.keys
response = gets.chomp
while cats.include?(response)
puts "The cat you chose is #{cats[response].age} old"
puts "The cat you chose is named #{cats[response].name}"
puts "The cat you chose is a #{cats[response].breed} cat"
puts "Is there another cat would you like to know about?"
response = gets.chomp
end
There isn’t a “while…else”, as far as I know. If you do not mind the loop continuing regardless of whether the response is a valid cat name or not, maybe this will work for you:
This will repeatedly prompt the user for a cat name, until the user responds with an empty string, at which point it will break out of the loop.