9/10 in "A night at the movies" : What is the problem in my code?


#1

movies = {
Memento: 3,
Primer: 4,
Ishtar: 1
}
..
.
.
.
when "delete"
puts "which movie"
title = gets.chomp
if ( movies[title] == nil )
puts "There is no movie"
else
movies.delete(title)
end

RESULT:

What do you want?
delete
which movie
Memento
There is no movie
nil

However there is a movie named as Memento, why the result is "There is no movie"?


#2

Probably because your hash is composed of symbols as keys, and you send a string to make the comparison.

Try with movies[title.to_sym] == nil instead.


#3

this is the whole code I used:
movies = {
Memento: 3,
Primer: 4,
Ishtar: 1
}

puts "What would you like 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 'delete' to delete a movie."

choice = gets.chomp.downcase
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 a number 0 to 4.)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been added with a rating of #{rating}."
else
puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
end
when 'update'
puts "What movie do you want to update?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "Movie not found!"
else
puts "What's the new rating? (Type a number 0 to 4.)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been updated with new rating of #{rating}."
end
when 'display'
movies.each do |movie, rating|
puts "#{movie}: #{rating}"
end
when 'delete'
puts "What movie do you want to delete?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "Movie not found!"
else
movies.delete(title.to_sym)
puts "#{title} has been removed."
end
else
puts "Sorry, I didn't understand you."
end


#4

still does not work............................


#5

sorry, i don't know the problem since you just put some part of your code.

here's my whole code. it's working on me. Hope it can help you

movies = {
"aadc" => 4
}

puts "What would you like 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 'delete' to delete a movie."
puts "input the option : "
choice = gets.chomp.downcase

case choice
when 'add'

puts "What's the title?"
title = gets.chomp.downcase

if movies[title.to_sym].nil?

    puts "what's the rating? (range : 0-4)"
    rating = Integer(gets.chomp)
    
    movies[title.to_sym] = rating.to_i
    puts "#{title} with #{rating} rating has been added!"

else
    puts "#{title} has already in the cfatalogue"
end

when 'update'

puts "What's the title that you want to update?"

title = gets.chomp.downcase

if movies[title.to_sym].nil?
puts"what's the new rating? (range: 0-4)";
rating = gets.chomp;

  movies[title] = rating;
  puts "#{title} has been updated!";

else
puts "Sorry, the movie is not exist yet";
end

when 'display'

movies.each {|tit, rat|
puts "#{tit}: #{rat}";
}

when 'delete'

puts "What the movie's title to delete?"
title = gets.chomp.downcase;

if movies[title.to_sym].nil?
puts "sorry, #{title} is not exist yet";
else
movie[title.to_sym].delete!;
puts "#{title} has been deleted";
end
else
puts "Error!"
end;