Error! Error


#1

https://www.codecademy.com/en/courses/ruby-beginner-en-0i8v1/0/6

Hi,

My code is passing, but when I enter a movie already in the hash, it acts as if its nil, and says its storing it. How do I get it to look at whats in the hash?

movies = {
    "scarlet" => 4,
    "romanholiday" => 3
}

puts "What would you like to see? Put add, update, display or delete"
choice = gets.chomp

case choice
    when "add"
    puts "What movie would you like to add?"
    title = gets.chomp
    title= title.to_sym
    if movies[title.to_sym].nil?
    puts "What do you rate the movie?"
    rating = gets.chomp
    rating = rating.to_i
    movies[title.to_sym] = rating.to_i
    puts "#{title} has been added with a rating of #{rating}"
    else puts "#{title} already exists"
    end
    when "update"
    puts "Updated!"
    when "display"
    puts "Movies!"
    when "delete"
    puts "Deteled!"

#2

This is a bit ambiguous from the way the exercise is phrased.

You must recreate your movies hash as symbols and then your code should work.

So you should have

movies = {
scarlet: 4,
romanholiday: 3
}

Also, just for clarity, you don't need
movies[title.to_sym]
as title is already in symbol format, so just
movies[title]
should suffice.


#3

This will not work. When the hash is written as in the above OP, it is not detected so permits us to add 'Scarlet' (I capitalized my test key) and a rating, but then reports this error from the SCT:

It looks like Scarlet wasn't added to the movies hash with a rating of 4.

These are not symbols but can be written as symbols in either form:

    Scarlet: 4,
    :'Roman Holiday' => 3

Note that keys with white space cannot be written in the first form.


#4

Supplementary comments:

Our program should expect typical film names with more than one word and proper name capitalization. It may not expect 'scarlet' (though that's a possible input) and won't expect 'romanholiday'.

Not many film titles are written in e.e.cummings style lowercase. They are proper names, and in the normal, capitalized.

movies = {
  Scarlet: 4,
  :'Fast and the Furious 5' => 10,
  :'Roman Holiday' => 3,
  Babel: 5,
  Crash: 4
}

Sample run...

Enter an action [add]
 add
Add a movie
 Roman Holiday
Movie already added
{:Scarlet=>4, :"Fast and the Furious 5"=>10, :"Roman Holiday"=>3, :Babel=>5, :Crash=>4}
nil

#5

Having problems with this as well.

movies = {
    :'The Matrix' => 5,
    :'The Artist' => 5,
    :'The Girl With the Dragon Tattoo' => 3,
    :'Bladerunner' =>  4,
    :'Crash' => 4
}

puts "What would you like to do?"
choice = gets.chomp

case choice

when "add"
    # Asking for user's input
    puts "Sweet!  Put a movie title that you think is worthy to be added to the list!"
    title = gets.chomp
    if movies[title.intern].nil?
    # Asking for the user's rating
    puts "Very nice. What rating (Between 1 and 5) would you give this film?"
    rating = gets.chomp
    # Adding user input + rating to movies hash
    movies[title.intern] = rating.to_i
    puts "Ok your movie and the rating you gave it has been added!"
else
    puts "Sorry Dood! Movie #{movie[title]} is in this list. The rating for it is #{movie[rating]}.  Add another ya slag!"
end

I had another question where I had problems creating symbols in the new ruby 1.9 method until I resolved it after sleeping on it. Let me try that here and post my results.


#6

Yep. Still no bueno.

movies = {
    The_Matrix:  5,
    The_Artist: 5,
    The_Girl_With_the_Dragon_Tattoo: 3,
    Bladerunner: 4,
    Crash: 4
}

So changing the symbols to this, and then when I run the code if I type "The Matrix" it will treat it as it's own input and add it in the hash. Because I understand that the underscore is 'counted' as part of the string than _. So how do I add in movies that has a space in their title and have the code still run and ... fail if it detects that the symbol already exists in the hash.

Man this is doing my head in.


#7

What error message are your getting from the SCT? Your first version of the hash looks to be correct as far as validity goes. If the SCT is kicking an error, then perhaps it does not expect the synonym, intern of to_sym. Could that be a possibility?


#8

I keep getting invalid local variable. Movies.

No matter what I do this is the output.


#9

So after rereading what @mtf had written, I've revised my symbols:

movies = {
    :'The Matrix' =>  5,
    :'The Artist' => 5,
    :'The Girl With the Dragon Tattoo' => 3,
    :'Bladerunner' => 4,
    :'Crash' => 4
}

puts "What would you like to do?"
choice = gets.chomp

case choice

when "add"
    # Asking for user's input
    puts "Sweet!  Put a movie title that you think is worthy to be added to the list!"
    title = gets.chomp
    if movies[title.to_sym].nil?
    # Asking for the user's rating
    puts "Very nice. What rating (Between 1 and 5) would you give this film?"
    rating = gets.chomp
    # Adding user input + rating to movies hash
    movies[title.to_sym] = rating.to_i
    puts "Ok your movie and the rating you gave it has been added!"
else
    puts "Sorry Dood! Movie #{movies[title]} is in this list. The rating for it is #{movie[rating]}.  Add another ya slag!"
end
    
when "update"
    puts "Updated!"
when "display"
    puts "Movies!"
when "delete"
    puts "Deleted!"
else
    puts "Error!"
end

This is my current code and after I type 'add' and enter Bladerunner, I get this output:

Codecademy passes, but I know this is wrong. I'm supposed to get an error stating that 'Bladerunner' is already added. Instead, there is a problem with my hash with my symbols.

I thought it was if movies[title.to_sym].nil? but looking at the first exercise, the code there and my code looks similar.

Doing my head in troubleshooting this so I'm just going to retype everything from scratch and see what happens.


#10

Figured it out!!

Didn't have to rewrite the whole thing after all. Turns out it's a typo.

I had:

The rating for it is #{movie[rating]}. Add another ya slag!"

instead of:

The rating for it is #{movies[rating]}. Add another ya slag!"

It was missing the 's' at the end.

May this one was crazy lol. Just that one typo lol.

The output now works in regards to giving me an error that the movie already exists, now I just need to fix the code where it output the movie rating. That should be easy.

Thanks for the help everyone!!!


#11

Which is evident in the error that was raised. undefined local variable ... movie. Mine those error messages for clues. They are usually right there in front of us.


#12

Agreed. I keep looking over it without even noticing. Took a search just to find it. Thanks for your help with this @mtf. Appreciate it bro.