These my code is incorrect?


#1

Hi!
I'm stacked at Ruby Course, In "7 update" chapter of "Night at the Movies"

on the console.
it is actually indicated that my code is correct

What should I do?
 update
enter the title
 spiderman
we found spiderman! wanna change rating? (y/n)
 y
add new rating
 199
spiderman's rating is just updated to 199!
nil

but there is error message

Oops, try again. It looks like spiderman wasn't updated with a new rating of 4.

and here is what i programmed

movies = Hash.new
movies = {
    'The Dark knight' => '5',
    :spiderman=>2
}

puts "What should I do?"
choice = gets.chomp

case choice
when "add"
    puts "What is the title?"
    title = gets.chomp.to_sym
    if movies[title.to_sym] == nil
        puts "and the rating"
        rating = gets.chomp.to_i
        movies[title] = rating
    else
        puts "the movies is already stored"
    end
    puts movies
when "update"
    puts "enter the title"
    title = gets.chomp
    if movies[title.to_sym] == nil
        puts "no hit"
    else
        puts "we found #{title}! wanna change rating? (y/n)"
        yn = gets.chomp
        if yn == 'y'
            puts "add new rating"
            rating = gets.chomp.to_i
            movies[title] = rating
            puts "#{title}'s rating is just updated to #{movies[title]}!"
        else
            puts "ok aborted"
        end
    end

when "display"
    puts "Movies!"
when "deleted!"
    puts "Deleted!"
else
    puts "Error!"
end

https://www.codecademy.com/en/courses/ruby-beginner-en-0i8v1/resume?curriculum_id=5059f8619189a5000201fbcb


#2

Throughout your code, you are using title.to_sym, but right in the end, when you are supposed assign the rating, you do this:

movies[title] = rating

Can you figure out what's missing? :wink:

Additionally, in order to avoid such mistakes later on and also optimize your code, it is better to just do this:

title = gets.chomp.to_sym # convert to symbol the moment you take input

and then just use title alone elsewhere in your code.

(In fact, you are already doing that in the when "add" block)


#3

Ohh!
I overlooked!

Thanks a lot!

I can step into next chapter thanks to your advise!