Error! Error!


#1

Hi all,

I have been stuck on this exercise for ages. I'm new to coding and I am getting through most by reading all the available info on the forums and learning as I go. In this case I've gone through every thread and I still can't work out why my code will not give me the message that "This movie already exists" when a movie is duplicated. I can 'pass' the exercise, but that is not the point. I want my code to do as it should.

Please can you help? (if someone could point out excactly what is wrong with my code I'd really appreciate it).

movies = { 
    Jaws: 5,
    Big: 4,
    Rambo: 2
}

puts "What do you want to know about movies?"
choice = gets.chomp

case choice
when "add"
puts "What movie would you like to add?"
title = gets.chomp.downcase
title = title.to_sym
if movies[title.to_sym].nil?
    puts "what rating would you give this movie 1-5?"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
    puts "#{title}, has been added with a rating of #{rating}."
else 
    puts "This movie already exists"
end 

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

#2

You have the wrong string and code beneath this statement,

You are supposed to check if movies[title.to_sym].nil? and then add it to your movies dictionary and tell the user it has been added so it must be set up something like,

if movies[title.to_sym].nil?
    movies[title.to_sym] = rating.to_i
    puts "#{title}, has been added with the rating of #{rating}."
else
    puts "That movie is already there :)"
end

#3

Thank you.

I have adjusted my code to:

movies = {
Jaws: 5,
Big: 4,
Rambo: 2
}

puts "What do you want to know about movies?"
choice = gets.chomp

case choice
when "add"
puts "What movie would you like to add?"
title = gets.chomp.downcase
title = title.to_sym
if movies[title.to_sym].nil?
movies[title.to_sym] = rating.to_i
puts "what rating would you give this movie 1-5?"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title}, has been added with a rating of #{rating}."
else
puts "This movie already exists"
end

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

But it still doesn't work. Have I followed your instructions accurantly?


#4

Craked it!!!

I replaced:

title = gets.chomp.downcase
title = title.to_sym

with

title = gets.chomp.to_sym

and now it works!


#5

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