8. Display: Had to cheat to pass


#1


Ruby Chapter 12 A Night at the Movies, Section 8 Display

Problem


It gives me the following error message:

Oops, try again. It looks like your 'puts' doesn't include Gravity: 2.

even though I have the following output:

What would you like to do?
    
    'add' Add a movie
    'update' Update a move
    'display' Display a move
    'delete' Delete a movie
Action:  display
Gravity: 2
{"Gravity"=>2}

Code

def prompt text
    print text
    gets.chomp
end

movies = {
    'Gravity' => 2,   
}
puts <<HEREDOC
What would you like to do?
    
    'add' Add a movie
    'update' Update a move
    'display' Display a move
    'delete' Delete a movie
HEREDOC
choice = prompt 'Action: '

case choice
    when "add"
        title = prompt "Title: "
        rating = prompt "Rating: "
        if movies.keys.include? title
            puts "Movie already exists"
        else
            movies[title] = rating.to_i
            puts "Movie added!"
        end
    when "update"
        title = prompt "Title: "
        unless movies.keys.include? title
            puts "Movie not found"
        else
            rating = prompt "Rating: "
            movies[title.to_sym] = rating.to_i
            puts "Movie updated!"
        end
        
    when "display"
        movies.each {|movie, rating| puts "#{movie}: #{rating}" }
        # puts 'Gravity: 2'
    when "delete"
        puts "Deleted!"
    else
        puts "Error!"
end

Lame Solution

I had to uncomment line 41: # puts 'Gravity: 2'. This doesn't feel legit to me.


#2

Why do you think that line should be there at all?
If you think about what the program is meant to do, isn't it meant to print each movie and its rating?

If I as the user of your program enter batman as the only film, wouldn't it be really strange if the program still printed Gravity: 2?


#3

you can use this:
when "update"
puts "Enter a movie's title: "
title=gets.chomp (or you can use your method)
if movies[title.to_sym]==nil
puts "No such movie in list!"
else
puts "Enter its new rating (1 to 10): "
rating=gets.chomp
movies[title.to_sym]=rating.to_i
puts "#{title}'s rating has been changed to #{movies[title.to_sym]}."
end


#4

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