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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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.
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!!!
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
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
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}"
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
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.