Error! Error!


#1

Please help me what I am doing wrong.

1movies= {
2 'braveheart'=> 10,
3 'starwars'=> 3
4}
5puts "my fav movies"
6choice=gets.chomp
7case choice
8
9when "add"
10puts "What is your fav movie?"
11title= gets.chomp
12
13if movies[title].nil?
14puts "What is the rating? 1 to 10"
15rating=gets.chomp
16movies[title.to_sym] = rating.to_i
17puts " The movie #{title} was added."
18
else
puts "This movie already exists."
end

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

When I enter starwars which I already have in my movies, I get below output.

my fav movies
add
What is your fav movie?
starwars
This movie already exists.
nil

But, at the bottom I get an error:

Oops, try again. Did you call .to_sym on title and .to_i on rating before adding them to the hash?**

If I enter if movies[title.to_sym].nil? instead of if movies[title].nil? at line 13 , then it doesn't realize that "starwars" is already in my movies so It just adds it. Please help.


#2

They're wanting to reassign the variable title and rating instead of doing it in place. Just add

title = title.to_sym
and
rating = rating.to_i
before you use them. you can take out the other calls also.


#3

In which line am I supposed to add them exactly? Thanks a lot.


#4

Any line before you actually add theme to the hash is fine.


#5

I put movies[title.to_sym] = rating.to_i after title=gets.chomp line and that is the only line it worked!


#7
movies = {
  Memento: 3,
  Primer: 4,
  Ishtar: 1
}

puts "What would you like to do?"
puts "Enter a command [add, update, display, delete]:"
choice = gets.chomp

case choice
when "add"
        puts "Add title"
        title = gets.chomp
    if movies[title.to_sym].nil?
        puts "Type rating"
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
        puts "Movie #{title} has been added with a rating of #{rating}."
    else
        puts "The movie #{title} 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


puts movies

#8

movies = { "steven seagal" => 8
}
puts "choice"
choice =gets.chomp
case choice

when "add"
puts "movie title"
title =gets.chomp.to_sym
puts "movie rating"
rating =gets.chomp.to_i
movies[title.to_sym] = rating.to_i
if movies[title.to_sym] == nil
movies[title.to_sym] = rating.to_i
else
puts "the movie exists"
end
puts "pair was added"
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end
that code is work .. does it's correct one ?