Lesson 6. Error! Error!


#1



Help! I am on lesson 6. Error! Error!


Codecademy says it works, but it isn't working.
This is my code:

movies = {
    zootopia: 5,
    inside_out: 5,
    pk: 5
}
puts "What would you like to do?"
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"
    if movies[title.to_sym].nil?
        puts "Enter your movie title:"
        title = gets.chomp
        puts "Enter the rating:"
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
            puts "#{title} has been added with a rating of #{rating}."
    else
        puts "There is already a movie called #{movies[title.to_sym]}!"
    end
    when "update"
        puts "Updated!"
    when "display"
        puts "Movies!"
    when "delete"
        puts "Delete!"
    else
        puts"Error!"
end

When I type in "add", it says "There is already a movie called 5!" I have no idea how this is happining....

I also tried this code... :

   movies = {
zootopia: 5,
inside_out: 5,
pk: 5
}
puts "What would you like to do?"
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.downcase.chomp

case choice
when "add"
puts "What is the title of the movie?"
title = gets.chomp
title.capitalize!
if movies[title.tosym].nil?
puts "What is the rating of the movie (0 through 4)?"
rating = gets.chomp
movies[title.tosym]=rating.to_i
puts "Movie was added! The movie is #{movies[title.tosym]} and its rating is #{movies[rating]}."
else
puts "That movie is already rated!"
end
    when "update"
        puts "Updated!"
    when "display"
        puts "Movies!"
    when "delete"
        puts "Delete!"
    else
        puts"Error!"
end

But it got stuck at movies[title.tosym]=rating.to_i saying "Undefined method "tosym" for (the input I put) :String". ( I found this code in the Q & A forum ). I am guessing that the =rating.to_i part after the movies[title.tosym] caused the error because it worked fine in the if movies[title.tosym].nil? statement.


#2

I also don't get what tosym means.


#3

.to_sym -> convert a value to a SYMBOL


#4

The problem is that you check if th movie exists in the hash without reading what title is. You need to read the input from the user and save it title before the if statement.
case choice
when "add"
puts "Enter your movie title:"
title = gets.chomp
puts "Enter the rating:"
rating = gets.chomp
if movies[title.to_sym].nil?
movies[title.to_sym] = rating.to_i
puts "#{title} has been added with a rating of #{rating}."
else
puts "There is already a movie called #{movies[title.to_sym]}!"
end


#5

This was so helpful ! I was stuck for a whole day on this line: movies[title.to_sym] = rating.to_i LOL

Thanks !


#6

I tried that...It got me through most of it, but then I typed in a rating of 5.5, and it said that there is already a movie called "5", even though I didn't type in the movie 5 but a movie that was in the movies hash.

It is also letting me pass. I don't know why. I think it has to do with the last line in the if statement (puts "There is already a movie called #{movies[title.to_sym]}!")


#8

Never mind...
I just made the .nil? into = nil ( so it is if movies[title.to_sym] = nil ). Then, since it was not reading the movie that I typed, I changed puts "There is already a movie called #{movies[title.to_sym]}!" into puts "That movie already exists!"
I have no idea how the = nil worked.

Another thing I could have done: keep the .nil? and replace the puts "There is already a movie called #{movies[title.to_sym]}!" into puts "That movie already exists!"


#9

Now, when I put a title that doesn't exist, it says that "This movie already exists" and also the error says the title I typed in wasn't added. THIS IS SO FRUSTRATING!!!!!!!!!!!!!!!!!!!!

Really SORRY, I Put movies["#title.to_sym"] = rating.to_i instead of movies[title.to_sym] = rating.to_i!! I AM SOOOOOO DUMB!!!


#10

Hi ayushisgr8!

try typing this:

choice = gets.chomp
case choice

when 'add'
puts "Type here your movie title!"
title = gets.chomp
if movies[title.to_sym].nil?
puts "This movie will be added"
puts "Please rate here movie"
rating = gets.chomp
movies = {}
movies [title.to_sym] = rating.to_i
puts "Your movie was added"
else
"This movie already exist"
end

it worked for me! If condition goes just after asking for title, otherwise wouldn`t work