9. Delete!


#1


instructions:
Go ahead and remove the puts "Deleted!" when the user types "delete".
Get the title from the user.
Include an if/else statement that puts an error if the movie's not in the hash; if it's there, use .delete to remove it as shown above.


I pass the challenge but the code is not quite right.

my code:

movies = {
    the_vampire_diaries: 4,
    forrest_gump: 5,
    this_christmas: 4,
    austin_powers: 1,
}
puts "What would you like to do?"
input = gets.chomp
choice = input

case choice
when "add"
    puts "Add movie here"
     title = gets.chomp.to_sym
if movies[title.to_sym].nil?
    puts "What's it's rating?"
     rating = gets.chomp.to_i
     movies[title] = rating
    puts "#{title} has been added with a rating of #{rating}"
else
    puts "That movie already exists!"
end

when "update"
    puts "What's your movie title?"
    title = gets.chomp
    if movies[title].nil?
        puts "Error!"
    else
        movies[title] = rating
    end
when "display"
    movies.each do |movie, rating|
        puts "#{movie}: #{rating}"
    end
when "delete"
    puts "Which title would you like to remove?"
    title = gets.chomp.to_sym
    if
        movies[title.to_sym].nil?
        puts "That's not in the hash!"
    else
        movies.delete(title.to_sym)
        puts "That movie has been deleted"
    end
else
    puts "Error!"
end

When I enter 'Forrest Gump' it still tells me that the movie is not in the hash when it clearly is! Please help.


#2

Check the first lesson for an example to follow. The author does not convert gets.chomp to symbol. That is done in the hash reference:

if movies[title.to_sym]

which makes better sense, at least to me.

When Ruby converts to symbol, does it insert underscores and convert to lower case? I kind of doubt that it does so you will need to study that concern.

Dug around a little and found this discussion in the Symbols topic:

https://discuss.codecademy.com/t/problem-with-1-9-symbol-syntax-rocket-has-landed/53036

And this touches on the subject, as well:

https://discuss.codecademy.com/t/dealing-with-multi-word-titles-repeating-program/53530/2


#3

Thanks! That helped a lot!


#4

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