6.Error


#1

movies = {"Lion_King"=> 8, "Aladin"=> 9, "Dragon_ball"=> 10}
puts "What you want 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 is the rating?"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "It is added"
else puts "It is already exist!"
end
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end

Can someone please help to check what's wrong with the codes? It is fine if the title i I key is not in the string, but not for those already exist.


#2

Im not sure wha is wrong with your code but this one worked for me

movies = {
:'Fast and the Furious 5' => 10
}
puts "do you want to add?"
choice = gets.chomp!
case choice
when "add"
puts "Add a movie"
title = gets.chomp
puts "Give a rating"
if movies[title.to_sym].nil?
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "The Movie was added!"
else
puts "Movie already added"
end
end


#3

Thanks. After I tried multiple times I found out that if movies[title].nil? worked instead of if movies[title.to_sym].nil?,

movies = {"Lion_King"=> 8, "Aladin"=> 9, "Dragon_ball"=> 10}
puts "What you want to do?"
choice = gets.chomp
case choice
when "add"
puts "What movie do you want to add?"
title = gets.chomp
if movies[title].nil?
puts "What is the rating?"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "It is added"
else puts "It is already exist!"
end
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end


#4

Yes...i tried doing the same....i.e removed .to_sym from the code.It just worked well with the output and said it already exists, but showed error saying the movie was not successfully added to the list when its already in the list.


#5

Did u remove both .to_sym? What I removed is only on the "if movies[title].nil?". And i kept "movies[title.to_sym] = rating.to_i". And then i tested it with 2 scenario, 1. try to add a new movie. 2.try to add an existing movie. Both worked well for my code..


#6

No not both.Just removed .to_sym from the if statement.Code works well and shows the desired output on the Output window but shows up error that movie was not added to the list.