3/10 The Case Statement


#1

Hi I don't know what is wrong with my code here can someone help me?

Here is the code:

movies = {
Star_Wars_The_Force_Awakens: 5,
Captin_America_Civil_War: 4.5,
Avengers_Age_Of_Ultron: 4.5,
}

puts "Welcome to the cinema showings list how may I help you today?"
puts "--Type 'add' to add a movie title"
puts "--Type 'update' to update movie ratings"
puts "--Type 'display' to view currently showing movies"
puts "--Type 'delete' to delete movie titles currently on the list"
choice = gets.chomp.downcase

case choice

when "add"
puts "Added"

when "update"
puts "updated"

when "display"
puts "Movies"

when "delete"
puts "Deleted"

else
puts "Error!"
end

Error message:Oops, try again. It looks like your case statement doesn't puts 'Added!' when choice == 'add'.

Output:add
Added
nil


#2

The lesson checker is expecting a ! on the string, Added.

Question:

What happens if a user enters, "Star Wars The Force Awakens"? It will not be found in the library. We can write white space in our keys if we use quotes:

"Star Wars The Force Awakens" => 4,

#3

Your code is perfectly fine except for the fact that you are printing "Added" instead of "Added!".


#4

Wow thanks i didn't even realize I had to put an !.
But I geuss that is how codeaccademy grades code. :smiley:


#5

I tried adding string keys to my movie hash and converting it to symbols, but it doesn't seem to be working properly during my if/else statement where it checks if the title is already in my hash. Do you know why? My if/else works fine when I use Inception because it is already a symbol, but not when I try Deadpool. My thinking is it must not be converting my keys to symbols, but I don't know why.

>     movies = {
>     "Dark Knight" => 9.5,
>     "Batman Begins" => 10,
>     Inception: 10,
>     "The Witch" => 9.4,
>     "Deadpool" => 9 
> }

>

movies.each { |title, rating| movies[title.to_sym] }

puts("This program gives you full control of the movie database.\nWhat would you like to do?\nYou can type 'add', 'update', 'display', or 'delete' to manage the database.")

choice = gets.chomp

> case choice
> when "add"
>     puts "What is the name of the movie you want to add?"
>     title = gets.chomp.to_sym
>     if movies[title].nil?
>         puts "What is the rating (out of 10) you would like to give to the movie?"
>         rating = gets.chomp.to_i
>         movies[title] = rating
>         puts "The movie #{title} has been added with a rating of #{rating}."
>     else
>         puts "#{title} is already in the database!"
>     end

#6

I think the issue arises when we directly convert the inputs to symbol. Try converting in the reference, instead...

title = gets.chomp
if movies[title.to_sym].nil?
    # ...
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i

#7

I gave that a shot, but it doesn't seem to change the outcome. Very confused.


#8

Missed this, earlier...

movies.each { |movie, rating| puts "#{movie}: #{rating}" }

#9

My original line of code was me trying to convert each string key to a symbol key. Is that not the way to do it?


#10

The iterator (each) will see it if it's a string or a symbol. We cannot mutate an iterator variable, which is what title.to_sym may be doing. Can't say for sure. Go back to lesson one if you need a full example to compare with.