A night at the movies "new syntax"


#1

Hello fellow coders!

In this exersize we are to make a program to organize our favourite movies.

I am stuck here:
04.Add that movie/rating pair to the movies hash and puts a message indicating the pair was added. (No need for to_sym or to_i just yet!)

When looking at what others have done and back to the example on the first page I see " ’’
’movies[title.to_sym].nil?
’’’
this is totally new to me. How does this add the key and values into the hash?

thanks


#2

HI can you post your code and a link to the lesson


#4

code is

when "add"
puts "movie title"
title = gets.chomp
if movies[title.to_sym].nil? 
    puts "what's the rating (type a raiting of 0 > 4)"
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
puts "#{title} has been added with a rating of #{rating}"
else "that movie has been added, its rating is #{movies[title.to_sym]"
end
end

this is a way of adding the keys and values to the movies hash: movies[title.to_sym] = rating.to_i``
how so?
and how does this:
#{movies[title.to_sym] ` returns the "title" from the hash once we have converted it into a symbol....

lesson : https://www.codecademy.com/en/courses/ruby-beginner-en-0i8v1/0/4?curriculum_id=5059f8619189a5000201fbcb


#5

If you follow the instruction it doesn't aske you to add a if statement
01. Inside your when "add" block, remove the puts "Added!" statement.
02. In its place, prompt the user for a movie title. Save the result in a new variable called title. (*Your code already has an example of how to do this!)

when "add"
    puts "Which movie do you want to add?"
    title = gets.chomp
end

03. Next, prompt the user for the rating of the movie. Save that in a new variable called rating.

when "add"
    puts "Which movie do you want to add?"
    title = gets.chomp
    puts "what's the rating (type a raiting of 0 > 4)"
    rating = gets.chomp
end

04. Add that movie/rating pair to the movies hash and puts a message indicating the pair was added. (No need for to_sym or to_i just yet!)

when "add"
    puts "Which movie do you want to add?"
    title = gets.chomp
    puts "what's the rating (type a raiting of 0 > 4)"
    rating = gets.chomp
    movies[title] = rating
    puts "#{title} has been added with a rating of #{rating}"
end

#6

thanks for replying wizmarco

With this code movies[title] = rating

Its tells me : "Oops, try again.
It looks like you didn't add to the movies hash!

What do I do...cannot pass go


#8

thanks wizmarco, you were right about the simpler (instructed way) way...


#9

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