4. Prompting Redux


I'm confused. When I enter this, it let's me start the next lesson. It doesn't let me input the movie I want to add or
the rating. I feel like I'm missing something.

movies = {
    Terminator_2:  4,
    Scarface: 3,
    Aviator: 3,
    Catch_Me_If_You_Can: 2,

puts "What would you like to watch?"
movies = gets.chomp

case movies

when "add"
puts "What movie would you like to add?"
title = gets.chomp
puts "How do you rate this movie?"
rating = gets.chomp
movies[title] = rating
puts "You gave #{title} a rating of #{rating}!"

when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
    puts "Error!"


What input are you giving it?

Here's what it looks like when I run it:

$ ruby daughter.rb 
What would you like to watch?
What movie would you like to add?
bob eats dinner
How do you rate this movie?
daughter.rb:18:in `[]=': string not matched (IndexError)
        from daughter.rb:18:in `<main>'

Seems like you need to rephrase your question, as it did let me enter a movie and a rating.


So I rephrased it, but it doesn't let me pass to the next lesson.

What would you like to do?
What movie would you like to add?
How do you rate this movie?
string not matched

I didn't realize I had to put in one of the prompts. I get "Oops, try again. It looks like you didn't add Moonstruck to the movies hash!"


Not sure what you want me to say. It asks if you added it. Did you?


Yes, well at least I thought I did. Do I add it to the list of movies already in the code in order for it to work? I thought it would add any movie when I run the code and type it in.


The program should ask to choose an action (add for example).
And since the exercise is about adding, the user should choose add.
Then a title and rating is entered by the user.
The program should then add that to the hash.

If the exercise at this point questions whether the entered movie was added, then you would need to find out whether it was. Doesn't really matter what you think will happen, that is a bad substitute for making observations about what does happen. You can do that by adding code that prints out the hash after it has been added, and then running it again with the same input. If it is indeed there then perhaps something wasn't done to specifications and you would need to study the instructions and compare to your program's behaviour. For the output you described above, printing the hash should show it having an entry of "Moonstruck"=>3
You could also add a test like:

puts "Is #{title} in #{movies} ? #{movies.has_key? title}"

Which would give an output like:

Is bob eats dinner in {"peanuts"=>1} ? false


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.