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


#1

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!"
else
    puts "#{title} has already been added!"
end

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

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

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.


#2

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?


#3

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.


#4

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?


#5

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.


#6

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!


#7

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?


#8

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


#9

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