3 The Case Statement


#1

Hi, could anyone please advise me, what's wrong with my code and how to fix it?
It returns a message "Oops, try again. It looks like your case statement doesn't puts 'Error!' when it gets unexpected input."


# favorite movie as the key, with a number rating for the value.
movies = {
    M1: 1,
    M2: 2,
    M3: 3
}

puts "What would you like to do?"
puts "-- Type 'a' to add a movie."
puts "-- Type 'u' to update a movie."
puts "-- Type 's' to see all movies."
puts "-- Type 'd' to delete a movie."

choice = gets.chomp.downcase
 # case statement decides what actions to take based on the user input
 # if/else are powerful, but we can get bogged down in ifs and elsifs if we have a lot of conditions to check. case statement is a great alternative if you have many conditions to check.
 
 case choice
 when "a"
     puts "Added!"
 when "u"
     puts "Updated!"
 when "s"
     puts "Movies!"
 when "d"
     puts "Deleted!"
 else
     puts "Error!"
end



#2

That's strange: once I entered something to make the code return "Error!", it did just that and gave me a pass! I didn't touch anything in the code. Why this happened?


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.