8. Display


#1




I have tried everything possible, from changing my initial hash to use symbols and printing the symbol to just plain cheating by using puts "the fountain: 5". I can't get this thing to recognize that I am putting the correct output. Any ideas?

Keep seeing the following error.
Oops, try again. It looks like your 'puts' doesn't include the fountain: 5.


movies = { "the fountain" => 5 }

print "Action: "
choice = gets.chomp

case choice
when "add"
    print "Movie: "
    title = gets.chomp
    title = title.to_sym
    print "Rating: "
    rating = gets.chomp
    if movies[title] == nil
        movies[title] = rating.to_i
        puts "Added!"
    else
        puts "Already rated"
    end
when "update"
    print "Movie: "
    title = gets.chomp
    title = title.to_sym
    if movies[title] == nil
        puts "Error!"
    else
        print "Rating: "
        rating = gets.chomp
        movies[title] = rating.to_i
        puts "Updated!"
    end
when "display"
    movies.each do |movie, rating|
        puts "#{movie}: #{rating}"
    end
when "delete"
    puts "Deleted!"
else
    puts "Error!"
end


#2

It may be best to follow the initial example and not convert title to a symbol directly, but in the reference

if movies[title.to_sym]

Unless we actually assign nil to an object, it doesn't exist for comparison, which is why, .nil?

if movies[title.sym].nil?
    movies[title.to_sym] = rating.to_i
else
    "The movie already exists in the database."
end

#3

While this might be better in some portions of the code, the problem is that display isn't registering as working properly by their system for some reason. :frowning:


#4

Fix all your code from the top down. Check the example in lesson 1 if you need some hints.


#5

First off, display doesn't have anything to do with the other case statements, so I don't know that it's not affecting my results on this particular lesson especially since it's not being called. However, after I updated my code with your suggestion nothing changed. So I went for literally copying and pasting the code from the lesson 1 example into my display case, it still gave me the same error. I swapped out my hash for the one from lesson 1 and while it still gave me the same error it seems to have decided I can move on. I'm not certain what's up with this particular lesson, but something seems to be off in their checking.


#6

This is a first, as far as I know. This module has shown very little in the way of bugginess over the past three+ years. There is little chance anything will be fixed as this course is set to sunset in the upcoming future, to be replaced with a newer version. Sorry I couldn't have been more help.


#7

Having the same problem. Literally copied and pasted from the example and still getting error advising me to put to_sym and to_i in my code. Kind of at a loss at this point. Did you find a solution?


#8

I just did some testing and it appears that once again, I am wrong.

if movies[title.to_sym] == nil

does work as expected, as does this,

if movies[title.to_sym] == nil then

We need to see nil for what it is, though, does not exist, a condition or state of being, not an object that can be compared. Imagine Browning's Sonnet,

Shall I compare you to *nothing*?

Makes little sense, hence the author's approach,

if movies[title.to_sym].nil?

which does not imply an object being present. Kind of twisted but we can wrap our heads around it.


#9

Back to the opening post, I just ran it in lesson 8 and got this error message from the SCT:

Oops, try again. It looks like your 'puts' doesn't include the fountain: 5.

The output in the console is,

Action:  display
the fountain: 5
{"the fountain"=>5}

Clearly there is a problem, but where?

Found it. The opening line,

print "Action: "

When we change print to puts the display lesson passes.

Still recommend going through the code and matching it up to example in lesson one as far as where to convert title to a symbol.


#10

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