Error! Error!


#1


Hi All,

I seem to be a bit stuck here and not sure exactly why... In the exercise Error! Error!, I have specifically added the movie already in the database, however, the code works and accepts that I can add it to the database (again), instead of 'This movie has already been added!' (which I was expecting).

Any advice would be much appreciated....

What is your favourite movie?
add
Which movie would you like to add?
Lock Stock
What is the rating of the movie?
4.0
Lock Stock has been added with the rating 4!
nil

movies = {
    "Lock Stock" => 4.5
}
puts "What is your favourite movie?"
choice = gets.chomp

case choice 

when "add"
puts "Which movie would you like to add?"
title = gets.chomp.to_sym 

if movies[title.to_sym].nil?
    puts "What is the rating of the movie?"
rating = gets.chomp.to_i
movies[title] = rating
puts "#{title} has been added with the rating #{rating}!"

else 
    puts "This movie has already been added!"
    
end

when "update"
puts "Updated!"
when "delete"
puts "Deleted!"
else
    puts "Error!"
end

#2

That pinpoints the problem to your test of whether it's already known, so you would continue investigating that.


#3

Thanks ionatan. It appears that I did not set "Lock Stock" as a symbol ( :"Lock Stock" ). Doing this, now means that the program recognises the initial input


#4

right. same data, but different types and therefore not equal


#5

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