No error message, but final code DOES NOT WORK!


#1

https://www.codecademy.com/en/courses/ruby-beginner-en-0i8v1/0/10?curriculum_id=5059f8619189a5000201fbcb

I am not getting any error messages with my code. What is the problem, then?

If I enter the name of a movie that is already in the database, I am prompted as if it were not in the database.

For example, if I type "add", then type "Annie Hall", I am prompted to give a movie rating: "Movie rating (scale of 0-4): ". The problem: "Annie Hall" is already in the database! So I should get a message saying "Whoops — that movie is already in the database!"

I went line by line through my code versus the exemplar code and could find no differences beyond the superficial (i.e. different user prompt language). Did I miss something? What is the issue here? As I said, I am not getting any error messages, and my code passes just fine. It's just... Not working correctly.

Thanks for any insight!

movies = {
    Annie_Hall: 3,
    Jurassic_Park: 3
}

puts "What would you like to do? Type ADD, UPDATE, DISPLAY, or DELETE: "

choice = gets.chomp.downcase
case choice
when "add"
    puts "Movie title: "
    title = gets.chomp
    if movies[title.to_sym].nil?
        puts "Movie rating (scale of 0-4): "
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
        puts "Database updated!"
    else 
        puts "Whoops — that movie is already in the database!"
    end   
when "update"
        puts "Movie title: "
        title = gets.chomp
    if movies[title.to_sym].nil?
        puts "That movie isn't in the database yet!"
    else 
        puts "Updated rating (scale of 0-4): "
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i   
        puts "Database updated!"
    end
when "display"
    movies.each do |title, rating|
        puts "#{title}: #{rating}"
    end
when "delete"
    puts "Movie to delete: "
    title = gets.chomp
    if movies[title.to_sym].nil?
        puts "That movie isn't in the database!"
    else
        movies.delete(title.to_sym)
        puts "Deleted!"
    end
else
    puts "Sorry, I didn't understand you."
end

#2

@hannahjm,

Have a close look at

"Annie Hall"

AND

Annie_Hall


#3

Okay, thank you. Very simple! I had assumed that the .to_sym code would essentially insert the underscore for me. I guess not!


#4

Right, to include a space in a symbol, surround the "text" by quotes and use the rocket notation, as such:

movies = {
    :'Annie Hall' => 3,
    :'Jurassic Park' => 3
}

For some reason, it doesn't work with the newer notation (e.g. 'Annie Hall': 3 will give you an error message.)