6. Error Error - doesn't recognize added movies as added


Hello! I am stuck on 6 - I tried adding a movie that was already in the hash and the program treated it like a new movie, asking for a rating and everything. My code looks just like the first example - where am I going wrong?

movies = {
    "A Hard Day's Night" => 4

puts "To add a movie, type 'add'. To delete a movie, type 'delete'. To update a rating, type 'update'. To see a list of all movies and ratings, type 'display'."
choice = gets.chomp

case choice

when "add"
puts "What movie would you like to add?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "What do you rate this movie on a scale from 0 to 4?"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "Added!"
    puts "#{title} has already been added!"

when "delete"
puts "Deleted!"
when "update"
puts "Updated!"
when "display"
puts "Movies!"
    puts "Error!"


For example, I typed in "A Hard Day's Night", the movie I initially added to the hash. The program then asked me for the rating instead of returning "A Hard Day's Night has already been added!"

To be clear, the Codecademy site seems to think I passed this but I know the code isn't working properly.


What could possibly cause it?

Either it wasn't already there, or you're failing to detect it, right? And if you find out which is the case, then you know which part of the code to look closer at. So how can you devise a test to tell which one it is?


I know that it's failing to detect it, but my code at

if movies[title.to_sym].nil?

looks just like the example (which works). And I know it's there because I can see it clearly typed into the code.


What would have to be true in order for that condition to find a match? Would some two values have to be equal? Which two values? Are they? If not, how do they differ?


Ok I really am having trouble figuring this out even with your hints. Do you see where I went wrong? How can I fix it? I promise I will remember this for next time if you can just tell me where I went wrong.


You need to devise experiments that give you more information, and consider what observations you've made so far.

What happens if you add two key-value pairs to a hash, and they have identical keys?

What happens if you add two key-value pairs to a hash, and they have differing keys?

Is that an experiment you could execute? What outcomes are there? What would those outcomes tell you? Which outcome do you get?

If they are different, then what can you do to inspect them?

Get your labcoat and protective glasses on!


What if you use 5 and 5.0 as keys?
Are they the same? Do they represent the same thing? How do they differ? How can you tell them apart? How would you create one instead of the other? If they represent the same thing, can they be converted to one another and if so, how? Throw in "5" and "5.0" for good measure, are they different from the other two?


you have a STRING in your initial hash, and you are comparing it to a SYMBOL in your If statement :slight_smile:

Either make the hash with symbols initially, or do the ".nil?" check with a string, not s a symbol. After that you can convert it to_sym


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