A Night at the Movies: Delete


#1

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

I don't inderstand why it doesn't work in Delete. Please someone can help me. thank you

movies  = ["color purple" => 3,
            "sex and the city" => 3,
            "fast and furious" => 1]
puts "What do you prefer like movies?"
puts "-- Type 'add' to add a movie."
puts "-- Type 'update' to update a movie."
puts "-- Type 'display' to display all movies."
puts "-- Type 'delete' to delete a movie."

choice = gets.chomp

case choice
when "add"
    puts "Title: "
    title = gets.chomp
    if movies[title.to_sym].nil?
    puts "Rating: "
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
    puts "#{title} has just been added with the rating of #{rating}"
   else
    puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
  end

when "update"
     puts "What movie do you want to update?"
    title = gets.chomp
    if movies[title.to_sym].nil?
         puts "Error:Movie not found!"
  else
    puts "What's the new rating?(Type a number 0 to 4.)"
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
    puts "#{title} has been updated with new rating of #{rating}."
end

when "display"
    movies.each do|movie,rating|
    puts"#{movie}: #{rating}"
end
    
when "delete"
   puts "What movie do you want to delete?"

   title = gets.chomp
   if movies[title.to_sym].nil?
    puts "Movie not found!"
   
  else
    movies.delete(title.to_sym)
    puts "#{title} has been removed."
  end

else
      puts "Sorry, I didn't understand you."

end

#2

The main issue is that your program defines an array, not a hash:

movies = {

}

Cannot get it to delete a title, yet, so there must be another problem I'm not seeing yet. But with the hash fixed, the lesson will pass.


#3

Thank you so much!!! I was going crazy lol :sweat_smile::joy:


#4

I am confused can some one please post the answer


#5

Asking for answers is not how we do things, here. If you are stuck, go back to lesson one and study the example given.


#6

A post was split to a new topic: Problem with to_sym