Stuck on Error! Error!


#1

I'm having problems with this exercise (https://www.codecademy.com/courses/ruby-beginner-en-0i8v1/0/6?curriculum_id=5059f8619189a5000201fbcb).
The first issue is that if I run this code and try to add 'The Wind That Shakes The Barley', the code doesn't allow me to add the film, but neither does it execute the else statement. Instead, I get an error message saying: 'Oops, try again. It looks like The Wind That Shakes The Barley wasn't added to the movies hash with a rating of 4.' I don't understand why the else statement isn't being executed and also why the error message is mentioning a rating of 4 when the code doesn't even run to the stage where the user is prompted to input a rating?
Secondly, I have a more general question about using symbols instead of strings as hash keys. If you were to use symbols as keys, how would you preserve the formatting of the string? For example, the user isn't going to input a film name in the form Django_Unchained:, they would most likely enter it as 'Django Unchained'. Does using .to_sym automatically convert an underscore to a space and add a colon at the end? If so, it seems this would make it necessary to convert back and forth between symbols and strings so that the user's string is converted to a symbol and if they check to see if the hash contains a particular string, the symbol in the hash would have to be converted back to a string. Is this the most efficient way of doing things? There's no mention of this in the lesson guidance.


movies = {
    'The Wind That Shakes The Barley' => 5
}
puts 'Do you want to add, update, display, or delete a film?'
choice = gets.chomp

case choice
when 'add'
    puts 'What is the name of the film you want to add?'
    title = gets.chomp
    if movies[title].nil?
    puts 'What is the rating (1-5) of the film you want to add?'
    rating = gets.chomp.to_i
    movies[title] = rating
    puts 'Added film.'
    else
        'Film has already been added.'
    end    
when 'update'
    puts 'Added!'
when 'display'
    puts 'Movies!'
when 'delete'
    puts 'Deleted!'
end

puts movies


#2

You should probably change 'The Wind That Shakes The Barley' into something like the_wind_that_shakes_the_barley or theWindThatShakesTheBarley. See if that will help.


#3

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