Night at the movie


#1

Error, Error!

Although the code passes the exercise, I don't understand why, if I add Terminator to the console and give a movie rating of 3, it doesn't print the else statement of "movie rating already exists". someone please help

movies = {
    Terminator: 3,
   
   }
 
puts "What is your favourite movie?"  
choice = gets.chomp

case choice
when "add"
    puts "What movie do you want to add"
    title = gets.chomp
    puts "how would you rate this movie?"
    rating = gets.chomp
    movies[title.to_sym]=rating.to_i

if movies[title] .nil?
    puts "what is the movie rating? (rating should be 1 to 5)"
    rating = gets.chomp
    puts "#{title} has been added with a rating of #{rating}"
else
    "puts movie rating already exists!"


#2

I smell something in this line.

"puts movie rating already exists!"

Your puts- statement is inside a string. It could look like

puts "movie rating already exists!"

#3

You've got all the right pieces, just in the wrong places:

case choice
when "add"
    puts "What movie do you want to add"
    title = gets.chomp
    if movies[title.to_sym].nil?  # movie does not exist
         puts "what is the movie rating? (rating should be 1 to 5)"
         rating = gets.chomp
         movies[title.to_sym]=rating.to_i # add movie to hash here
         puts "#{title} has been added with a rating of #{rating}"
    else  # movie already exists
        puts "movie rating already exists!"
    end

#4

Thank you, that worked nicely


#5

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