FAQ: A Night at the Movies - Prompting: Redux!

This community-built FAQ covers the “Prompting: Redux!” 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 Prompting: Redux!

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!

I am having trouble adding a key/value for the pairs to the hash. Any tips?

1 Like

try the .store method :wink:

2 Likes

case choice
when “add”
puts “What movie would you like to add?”
title = gets.chomp
movies[title]
when “update”
puts “What is your rating for the selected movie?”
rating = gets.chomp
movies[title] = rating

Placing this code, above, adds the key/ hash pair. However, you will need to complete this key/ value syntax by converting them to a symbol(.to_sym) and integer(.to_i), respectively.

Hello, I am having the same problem. I am stuck in this step. Here is what my code looks like. It seems I am not adding the key/value pair to the movies hash. Not sure how to do it? Can anybody help me?thanks!

movies = {“good fellas”=> “5”}
puts “what’s your favorite movie?”
choice=gets.chomp
case choice
when “add”
puts “what’s the title of the movie?”
title=gets.chomp
puts"what’s the rating for this movie?"
rating=gets.chomp
when “update”
puts “Updated!”
when “display”
puts “Movies!”
when “delete”
puts “Deleted!”
else
puts “Error!”
end

I struggled with this too, but the member who said use the .store method is correct.

Adding

movies.store(key, value)

after rating=gets.chomp will see you right.

1 Like

can someone break this code down for me please, and tell me what it
movies[title.to_s] = rating

Hi @kwdm5389 and welcome to the forums! This part of the lesson focuses on adding a movie and its corresponding rating to your movies hash. When we enter the title and rating of our movie, they are passed through this line of code movies[title.to_s] = rating which stores them in our movies hash as a key/value pair. What its really doing is adding the title of the movie to the hash as a string, indicated by the .to_s method, and having it store the rating you entered. So, in this case, the title is our key and the rating is our value. Hope this helps, let me know if you need more clarification!

Hi,

what would happen if I didn’t have the .to_s?
what difference does it make as I removed it and it still works?

Can someone teach me how to add title/rating to movie hash?

Error message
Did you create a variable called rating?

link
https://www.codecademy.com/courses/learn-ruby/lessons/a-night-at-the-movies/exercises/prompting-redux

I am really stuck in this exercise.
I have already tried all options and different tips prompt here by other users, but no success!!!
I tried

  • movie[title]
  • movie[title] = rating
  • movie.store(title, rating)
  • movie.push(title)
  • movie[title] = rating
    And I aways receive the message “It looks like you didn’t add to the movies hash!”
    I don’t know what else to do!

1 Like

I ran into the same issue.

What I ended up doing was removing the final puts statement (line 19), then running the code. This seemed to work and for some reason met the requirement of the exercise.

I then added the put statement back it and ran it again. It then worked, and printed out the final line.

Instruction:
Add that movie/rating pair to the movies hash.
and the solution is given
movies[title.to_s] = rating
I didn’t really get this code!
kindly help me out Rubyists!

yeah same here abdulvahid…
movies[title.to_s] = rating
i get that youre converting the title to a string. but how does that = rating??

ok upon further research I think i figured out whats going on…
ruby is taking your “[title.to_s]” and saying this is your key in the new key/value pair and pairing it up with your other input, the “rating” and storing it in the movies hash…
check out this page on stack. https://stackoverflow.com/questions/6863771/how-to-add-to-an-existing-hash-in-ruby/6864345

1 Like

Not quite. It is a string, already. We’re converting it to a symbol.

You get now that it is a key which is paired up with a value. Symbols are very unique and cannot be duplicated. That makes them ideal for use as keys in a hash. I may be talking out my chimney, here… Ruby is better optimized for symbols than strings. It follows that a symbol is one thing, whereas a string is several characters. To be sure you have the right string you have to confirm all the characters. To be sure you have the right symbol is no issue at all. You either have it or you don’t.

Getting frustrated with this exercise. I don’t understand how to add a key, value to an existing hash

movies = {
  'Inglourious Basterds' => '5'
}

puts "What would you like to do?"

choice = gets.chomp

case choice
when "add"
puts "Go ahead, add a movie."
title = gets.chomp
puts "Nice. What would you rate this movie from 1-5?"
rating = gets.chomp
movies[title, rating]
when "update"
  puts "Updated!"
when "display"
  puts "Movies!"
when "delete"
  puts "Deleted!"
else
  puts "Error!"
end

Can anyone advise? Whats even more frustrating is that the solution includes the .to_s method which the exercise mentions “isn’t needed yet”.

You are very close. object [subscript] operator value.

Our movies hash is the object, and title is the subscript, as in a key. rating is the value. In an assignment the operator is =

movies[title] = rating
1 Like

Thank you for taking the time to respond. Unfortunately, I’m still stuck after applying the suggested change. I’m still getting an error saying “It looks like you didn’t add to the movies hash”

movies = {
  'Inglourious Basterds' => '5'
}

puts "What would you like to do?"

choice = gets.chomp

case choice
when "add"
puts "Go ahead, add a movie."
title = gets.chomp
puts "Nice. What would you rate this movie from 1-5?"
rating = gets.chomp
movies[title] = rating
when "update"
  puts "Updated!"
when "display"
  puts "Movies!"
when "delete"
  puts "Deleted!"
else
  puts "Error!"
end

First off. let’s comment out all the gets.chomp and use literal assignments to make sure the logic is working.

choice = "add"

...

title = "Babel"
rating = 4.5

movies[title] = rating

Has the symbol been brought up, yet?