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!
So the following is the code I’m using for this section of the exercise:
puts "What movie would you like to add?"
title = gets.chomp
puts "What is your rating for the movie? (Use 1-4)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "Your movie was added!"
My problem is this adds the key to hash as a symbol with quotation marks. For example: :“The Waterboy”. Is there a way to get the program to not add those quotation marks?
It involves changing all the space chararacters to an underscore. That will require more logic since we will ultimately need to search our database on normal titles. If our hash contains, say,
"A Night to Remember": 2
we can query the data for an exact match on the quoted string. If we use the quoted string and have the symbol written as,
A_Night_to_Remember
will the query return a match?
Our logic will need to examine all user inputs, run a conversion method on them, store them in the hash as symbols if not already there, or retrieve the symbol and its data, and convert back to a normal string so it can be matched to the input.
For now, one suggests leave the strings as they are presently converted. Once you finish the project, the more complex logic can be written into your code. It should be able to return 2 for a query on "A Night to Remember", even if the hash key is written as a snakecase symbol.
I’m getting an error again when using the bang method on downcase. When I remove the bang it works fine. The bang method also only works when any of the letters I type are uppercase.
How would I change it so that no matter how the input was put in (uppercase/lower or mixed) it would still work? I’m lost… In the previous post you responded to me saying we have to use the bang method.
When I run the code, it prompts me "What would you like to do? " and when I type add, it puts "What movie would you like to add?
What rating does the movie have? " at the same time. It doesn’t give me a chance to type in what movie I want to add. I thought it was a problem with my code, but when I clicked get help, and ‘get code solution’ and run that code, this error still happens. Doesn’t seem like anyone else is having this problem? Is this a bug or am I missing something?
It’s an old course in an old environment that is not supported any longer. The workaround is to not get user input, but assign literal values to the variables so you can see the program run through its paces. I know that is not much to offer, but it does see one across the line.
I ran the following code and I got the green check that it was correct, however the movie didn’t exist in the hash and the result said “That movie already exists”. So what did I do wrong?
movies = {
StarWars: 4.8,
Divergent: 4.7
}
puts "What would you like to do? "
choice = gets.chomp
case choice
when “add”
puts "What movie would you like to add? "
title = gets.chomp.to_sym
puts "What rating does the movie have? "
rating = gets.chomp.to_i
if movies[title.to_sym] = nil
movies[title] = rating
else
puts “That movie already exists!”
end
Output:
What would you like to do?
add
What movie would you like to add?
cinderella
What rating does the movie have?
9
That movie already exists!
1 case choice
2 when “add”
3 puts "What movie would you like to add? "
4 title = gets.chomp.to_sym
5 puts "What rating does the movie have? "
6 rating = gets.chomp.to_i
7 movies[title] = rating
I am having a hard time understanding what is the purpose of line 7. I do not understand it. It somehow tells me that the title of movies is now rating? is [title] a temporary holder? Please explain.