Issue in initial code provided for this activity


#1

Hello,

I posted earlier with a plea for help to help me find the bug in my if/else statements on Ruby for the Night at the Movies project. I restarted my project to work from scratch, and when I tried the model code provided at the opening of the project for reference, Ruby displayed the same bug. Again, the bug is that only the if statement appears in .nil? conditioning, even if the title is already listed in the hash, or, in other words, the else statement does not initiate even when the user input applies. Here is the original code from Ruby. What am I missing??:

Night At The Movies:

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

Again, I’ve moved through the project onto the next section, but am feeling very insecure about that bit of code not working and me not knowing WHY.


#2

Not sure what bug you refer to. Your code seems to run correctly…

What would you like to do?
-- Type 'add' to add a movie.
-- Type 'update' to update a movie.
-- Type 'display' to display all movies.
-- Type 'delete' to delete a movie.
 add
What movie do you want to add?
 Ishtar
That movie already exists! Its rating is 1.

#3

Hmm, when I ran it on my computer, it asked for the rating. I’ll try again. Thanks for looking into this with me.


#4

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