FAQ: A Night at the Movies - Error! Error!

This community-built FAQ covers the “Error! Error!” exercise from the lesson “A Night at the Movies”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Error! Error!

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Question: Why doesn’t it return the else statement when I add the movie Sound with rating 3? See the code below please. Thank you.

movies = {
Sound: 3,
Sleepless: 4
}
puts “What would you like to do?”
puts “- Type ‘add’ to add a movie and its rating.”
puts “- Type ‘delete’ to delete the record of a movie.”
puts “- Type ‘display’ to view all the movies.”
puts “- Type ‘update’ to modify a movie record.\n”
choice = gets.chomp
puts "\nYou typed: " + choice + “.” + " " + “Just a moment please…\n”

case choice
when “add”
puts “What is the title of the movie?”
title = gets.chomp + “\n”
if movies[title.to_sym].nil?
puts “What is the rating of the movie?”
rating = gets.chomp
movies[title.to_sym] = rating.to_i
else
puts “That movie already exists. It’s rating is: #{movies[title.to_sym]}”
end
when “update”
puts “Updated!”
when"display"
puts “Movies!”
when “delete”
puts “Deleted!”
else
puts “Error! That is not a valid option.”
end

Because you’re adding “\n” to the title, it does not already exist in the hash. If you were to print your hash after adding “Sound\n”, you would see this: {:Sound=>3, :"Sound\n"=>3}
Drop the + "\n", and your code should work as expected.

1 Like

Dear All,

I have some issues adding the if.

Till this moment everything runs ok.

image

But when I put If statement it gives me an error. See below

Thank you!

Think about the elements necessary for an if block. There seems to be something missing at the ‘end’. :wink:

1 Like

I spent hours trying to figure what is happening here, but unfortunately no luck.
Even validator says the code is OK, but i have only one movie in hash program is saying that movie alredy exists…! Please HELP!!!

Found and fixed problem.

rating = gets.chomp.to_i
if movies[title] == nil
movies[title.to_sym] = rating
else
puts “Movie already exists”
end

2 Likes

Does anyone know why we can’t use the following code?

  if movies[title.to_sym] != nil 

This one is correct:

  if movies[title.to_sym].nil? 

Your first snippet if movies[title.to_sym] != nil is working for me, when I swap it into my solution.

Note that your second snippet returns the opposite of the first:

foo = nil   # => nil
foo != nil  # => false
foo.nil?    # => true
My full solution:
movies = Hash.new
movies = { "Alien": 7 }
puts "Please choose an option."
choice = gets.chomp
case choice
  when "add"
    puts "Please enter a title."
    title = gets.chomp.to_sym
    # if movies[title.to_sym] != nil        # also works
    if movies[title] != nil
      puts "#{title} is already on the list."
    else
      puts "Thanks.  Now please enter a rating for #{title}."
      rating = gets.chomp.to_i
      movies[title] = rating
      puts "Added #{title} (#{movies[title]})"     
    end
  when "update"
    puts "Updated!"
  when "display"
    puts "Movies!"
  when "delete"
    puts "Deleted!"
  else
    puts "Error!"
end
2 Likes

Can someone tell me why the below code isn’t returning the add error for movies with multiple words in the title?

movies = {
  The_Last: 4
}

puts "What would you like to do?"
choice = gets.chomp

case choice
  when "add"
    puts "Please type out the movie title~"
    title = gets.chomp
    puts "What's the single-digit rating, 1-4?"
    rating = gets.chomp
      if movies[title.to_sym].nil?
        movies[title.to_sym] = rating.to_i
      else
        puts "That movie already exists!"
      end

puts movies

I also tried :The_Last => 4 but I’m getting the same thing - the movie gets added with quotes in the title somehow (confirmed by putting the hash at the end to review). This is only happening with titles larger than a single word - works totally fine otherwise. I’ve tried searching online to see if there was a problem with my syntax or something, but underscores are what we’re supposed to use for spaces in symbol names (like we learned earlier in the course)…so I have no idea what could be causing this and it’s driving me nuts D:>

Hi, I’m not sure if you are still stuck on here but…
I tried your codes and seems to me that your ‘if/else’ statement needs to come higher?
currently its under where you ask the user for the rating of the movie so that’s in the way for the ‘if/else’ statement to be able to check if the movie name does exist or not.

try and put your ‘if’else’ statement after this?
don’t forget to ask your movie rate after the ‘if?’ part

I’ve re-arranged underneath, just in case I was not clear in explaining but give it a ty before hand :slight_smile:

Summary
movies = {
  The_Last: 4}

puts "What would you like to do?"
choice = gets.chomp

case choice
  when "add"
    puts "Please type out the movie title~"
    title = gets.chomp
    if movies[title.to_sym].nil?
      puts "What's the single-digit rating, 1-4?"
      rating = gets.chomp
      movies[title.to_sym] = rating.to_i
    else
      puts "That movie already exists!"
    end     
end
puts "#{title}, #{rating}"

yeah I had the same issue. Forgot that with IF, you need to put == and not =

Here is my code

My question is, what if the movie title I already have on the list as a symbol is made of two words?
When I type it in with a space just to get a prompt saying it already exists it doesn’t work. Only when I type it Doctor_sleep

[codebyte]

Old post, but if anyone comes here wondering:

You could use title = title.gsub(" ", "_") to handle that situation. That way, whether you type your movie as Doctor Sleep or Doctor_Sleep, it doesn’t matter.

A couple of other notes if anyone has their code set up in a similar way and lands on this thread in the future:

There is a missing end after the if/else statement.

Perhaps it was not meant to be there or related to the question, but: you don’t need to create an empty movies hash at the bottom of this code; this will overwrite your original hash.