6/10 Code run, but it doesn't give outputs that "movie is already exist"


#1

I really wondering why it doesn't give a an output message like.
This would be my error message:
puts "That movie already exists! Its rating is #{movies[title.to_sym]}."

And this is my code:
movies = {
"Ironman" => 2,
"Avengers" => 2,
"Man of Steel" => 1,
"How to train your Dragon" => 2
}

puts "What would you like want to do?"
puts "--Type 'add' to add a movie."
puts "--Type 'update' to update a movie."
puts "--Type 'display' to display all movies."
puts "--Type 'remove' to remove a movie from the list."
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 number 1 to 5)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been added to the movie list with a rate of #{rating}"
else
puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
end

when 'update'
puts "Updated"

when 'display'
puts "Movies"

when 'delete'
puts "Deleted"

else
puts "Error!"
end

Could anyone explain me about, if there's any code I missed or what am I wrong.
Thanks :smile:


#2

I just had a similar problem, what error do you get? I managed to work it out by examining the error message.


#3

I didn't get any error. Code runs and I passed. I don't know why.
The instruction was try to add a movie in 'movies' and if the movie exist print the message 'That movie already exist'. And I try to 'add' existed movie but I didn't print the message that was 'That movie already exist'


#4

I have the same problem. I already have a movie called lotr exactly.
I add another with the same name and it just adds it anyway

movies= {"lotr"=>5}
puts "What do you wanna do"
choice=gets.chomp

case choice
when "add"
puts "Add a movie"
title=gets.chomp
if movies[title.to_sym].nil?
puts "Insert it's rate from 0 to 5"
rating=gets.chomp
movies[title.to_sym]=rating.to_i
puts "#{title} has been added with a rating of #{rating}."
else
puts "Movie already exists! Its rating is #{movies[title.to_sym]}"
end
end


#5

movies = {
:Mulan => 10
}

puts "Input one of the following: add, update, display, or delete"
choice = gets.chomp

case choice

when "add"
puts "Please input a movie:"
title = gets.chomp

if movies[title.to_sym] == nil
    puts "Give it a rating:"
    rating = gets.chomp
    rating = rating.to_i
    movies[title] = rating
else
    puts "Movie is already added"
end

when "update"
puts "Updated!"

when "display"
puts "Movies!"

when "delete"
puts "Deleted!"

else
puts "Error!"
end

Try this and input 'add' and then 'Mulan'. Turns out that you need to change your initial hash to a symbol so it can be compared using "if", and be sure to use a boolean operator