9. Delete, stuck


#1



In the exercise 9.Delete - Night At The Movies

Can anyone please point out where I did wrong?

They are showing: this error: (ruby):39: syntax error, unexpected keyword_when, expecting $end
when "delete"
^

movies = {
Inception: 4,
Home: 3,
Matrix: 4
}

puts "Type 'add' to add movie."
puts "Type 'update' to update."
puts "Type 'display' to display movies."
puts "Type delete to remove movies."

choice = gets.chomp
case choice
when "add"
puts "Type in A New Movie Name"
title = gets.chomp.to_sym
puts "What rating will you give your movie?"
rating = gets.chomp.to_i

if movies[title.to_sym].nil? 
movies[title] = rating
puts movies
puts "Your new movie and its rating have been added!"
end

when "update"
puts "Enter the movie title"
title = gets.chomp.to_sym
if movies[title].nil?
puts "Movie not found!"
else
puts "Enter new movie rating"
rating = gets.chomp.to_i
movies[title] = rating
puts "Movie #{title} rating was updated"
end
when "display"
movies.each {|movie,rating| puts "#{movie}: #{rating}"}
end
when "delete"
puts "What movie do you want to delete?"
title = gets.chomp.to_sym
if movies[title.to_sym].nil?
puts "Movie not found!"
else
movies.delete(title.to_sym)
puts "#{title} has been removed."
end
else
puts "Sorry, I didn't understand you."
end

#2

@arraysolver09650,

You either use the syntax
do code-block end
or
{ code-block } as you are doing with the each Method in "display"-clause

choice = gets.chomp
case choice
  when "add"
     puts "Type in A New Movie Name"
     title = gets.chomp.to_sym
     puts "What rating will you give your movie?"
     rating = gets.chomp.to_i
     
     if movies[title.to_sym].nil? 
        movies[title] = rating
        puts movies
        puts "Your new movie and its rating have been added!"
     end

  when "update"
     puts "Enter the movie title"
     title = gets.chomp.to_sym
     if movies[title].nil?
        puts "Movie not found!"
     else
        puts "Enter new movie rating"
        rating = gets.chomp.to_i
        movies[title] = rating
        puts "Movie #{title} rating was updated"
     end
   when "display"
     movies.each {|movie,rating| puts "#{movie}: #{rating}"}
     ##end either you use do--end of { }
   when "delete"
     puts "What movie do you want to delete?"
     title = gets.chomp.to_sym
     if movies[title.to_sym].nil?
        puts "Movie not found!"
     else
        movies.delete(title.to_sym)
        puts "#{title} has been removed."
     end
  else
     puts "Sorry, I didn't understand you."
  end