8. Display

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/en/courses/ruby-beginner-en-0i8v1/0/8?curriculum_id=5059f8619189a5000201fbcb#

<In what way does your code behave incorrectly? Include ALL error messages.>
I keep getting “(ruby):38: syntax error, unexpected $end, expecting keyword_end” error after I try to save and submit my code. I don’t see anything wrong with my code. Can anyone please help me figure out why Codecademy won’t let me through?

```

movies = {
Gladiator: 5
}
puts “Do you want to add, update, display or delete a movie?”
choice = gets.chomp
case choice
when “add”
puts “Add a movie”
title = gets.chomp
puts “What rating do you want to give this movie?”
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts “#{title} and its #{rating} rating has been added!”
else puts “This movie title already exists.”
end

case choice
when “update”
puts “Which movie to do want to update?”
title = gets.chomp
if movies[title] = nil
puts “The movie is not in the current database. Sorry.”
else puts “What rating do you want to give this movie?”
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts “Thanks!”
end

case choice
when “display”
movies.each { |movie, rating| puts “#{movie}: #{rating}” }
end

case choice
when “delete”
puts “Deleted!”
else
puts “Error!”
end

<do not remove the three backticks above>

You’re getting a few stuffs wrong. Here:

It should be:

   ` if movies[title.to_sym].nil?`

Hope this helps! :slight_smile:

Thanks. I made the change but it’s still giving me the same error. The error refers back to line 38, but I don’t see anything in that line that is wrong.

Okay,

The key in the hash should be in quote.

movies = {
   "Gladiator": 5
}

Those lines don’t look good.

when "display"
movies.each do |movie, rating|
    puts "#{movie}: #{rating}"
end

Didn’t work unfortunately. If I put the quotations around Gladiator, it says there’s an error in line 1. If I keep the status quo and then split the movies.each do line into the way you put it, the error in line 39 remains. Thanks for the help though.

It should be: choice = gets.chomp.downcase

And I noticed some, I would say, needless case choices

A full summary now is:

choice = gets.chomp.downcase

case choice

when "add"
puts "Movies title!"
title = gets.chomp

if movies[title.to_sym] = nil
puts "What rating do you want to give this movie?"
rating =  gets.chomp

movies[title.to_sym] = rating.to_i
puts "#{title} and its #{rating} rating has been added!"

else puts "This movie title already exists."
end

when 'update'
    puts "Which movie to do want to update?"
    title = gets.chomp
    if movies[title.to_sym].nil?
        puts "The movie is not in the current database. Sorry."
    else
        puts "What rating do you want to give this movie?"
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
        puts "#Thanks!"
    end

when "display"
movies.each do |movie, rating|
    puts "#{movie}: #{rating}"
end

when "delete"
puts "Deleted!"

else
    puts "Error!"
end

It worked! Thank you so much!

1 Like

whats wrong with this ?

the result is:-
{:kings=>1, :rings=>2, :chings=>5}
nil

it should be like:-
{kings:1,rings:2,chings:5}
nil

movies = {
kings:1,
rings:2,
chings:5
}
puts “What would you like to do?”
choice = gets.chomp

case choice
when “add”
puts “What movie do you want to add?”
title = gets.chomp
if movies[title.to_sym].nil?
puts “What’s the rating? (Type a number 0 to 4.)”
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts “#{title}: #{rating}”
else
puts “That movie already exists! Its rating is #{movies[title.to_sym]}.”
end
end

puts movies

case choice
when “update”
puts “Which movie to update?”
title=gets.chomp
if movies[title.to_sym].nil?
puts “error! this movie does not exist.”
else
movies[title.to_sym]
puts “what rating to update? (0-5)”
text=gets.chomp
movies[title.to_sym]=rating.to_i
puts “#{title}:#{rating}”
end
case choice
when “display”
movies.each do |movie, rating|
puts “#{movie}: #{rating}”
end

end
end

You’ve got two extra end here.

when 'display'
  movies.each do |movie, rating|
    puts "#{movie}: #{rating}"
  end