10 Nice Work!


#1



Why is it that when I add a movie that is already in the database, it would still ask the user to key in an input instead of displaying "The movie already already exist"


movies = {
    '17 Miracles' => 10,
    'Superman' => 10
    
}

puts "What would you like to do?"
puts "Please type one of the following according to your choice"
puts "add - Add a movie"
puts "update - Update a movie in the database"
puts "display - Display a movie of your choice"
puts "delete - Delete a movie in the database"

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} has been added with a rating of #{rating}."
  else
    puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
  end

when "update"
    puts "Enter the movie title:"
    title = gets.chomp
    if movies[title].nil?
        puts "The movie is not yet added in the database"
    else
        puts "Enter the new movie rating:"
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
    end
when "display"
    movies.each do |movie, rating|
        puts "#{movie}: #{rating}"
    end
when "delete"
    puts "What movie would you like to delete?"
    title = gets.chomp
    if movies[title].nil?
        puts "The movie is not yet in the database"
    else
        movies.delete(title.to_sym)
        puts "#{title} has been removed from the database"
    end
else 
    puts "Error!"
end


#2

Here is the problem after rating

rating = gets.chomp

you should have an if statement that checks if the movie is not in your hash, you put this if statement in the wrong place,

it should be after
rating = gets,chomp


#3

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