Error message indicates hash is not added, debug line indicates code is code


#1



It keeps telling me that I didn't save the additional information to the movies hash. However, at the end of my code I have a "puts movies" command to help me debug. the output of this command shows me that the hash IS being modified correctly.

movies = {
ran: 4,
aladin: 3.5
}

puts "What would you like to do? "
puts "Type 'add' to add a title "
puts "Type 'update' to modify a rating "
puts "Type 'display' to display a title "
puts "Type 'delete' to delete a movie "

choice = gets.chomp

case choice
when "add"
puts "Input the title of the movie you want to add: "
title = gets.chomp
movies[title]
if movies[title.to_sym].nil?
puts "What is the rating for this movie? "
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "The movie #{title} and rating #{rating} were added. "
puts movies
else
puts "Sorry the movie #{title} is already in the library."
puts movies
end
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end

puts movies


In the latest attempted I added the movie "Catch me if you can" with a rating of 5 as input. Debug output is as follows:

What would you like to do?
Type 'add' to add a title
Type 'update' to modify a rating
Type 'display' to display a title
Type 'delete' to delete a movie
add
Input the title of the movie you want to add:
Catch me if you can
What is the rating for this movie?
5
The movie Catch me if you can and rating 5 were added.
{:ran=>4, :aladin=>3.5, :"Catch me if you can"=>5}
{:ran=>4, :aladin=>3.5, :"Catch me if you can"=>5}
nil

The following is the error message I received:

Oops, try again. It looks like you didn't add Catch me if you can to the movies hash!

Is this because the title is saved in quotation marks (i.e "" )? If that is the cause how can I modify the code to remove those?


Replace this line with your code.


#2

This time I tried again with Robocop and a rating of 3.5 it seems that the quotations were due to the other movies being multi-word titles. the following is the debug output:

What would you like to do?
Type 'add' to add a title
Type 'update' to modify a rating
Type 'display' to display a title
Type 'delete' to delete a movie
add
Input the title of the movie you want to add:
Robocop
What is the rating for this movie?
3.5
The movie Robocop and rating 3.5 were added.
{:ran=>4, :aladin=>3.5, :Robocop=>3.5}
{:ran=>4, :aladin=>3.5, :Robocop=>3.5}
nil

*note I modified the code on line 22 from rating.to_i to rating.to_f thus allowing me to save a 3.5 rating.

I still received the same error message:
Oops, try again. It looks like you didn't add Robocop to the movies hash!


#3

It's checking whether the title is a key in your hash. Your hash only has symbol keys and your title is a string, in other words the title isn't a key in your hash.


#4

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