4. Prompting: Redux!


#1

Hi everyone! Could someone explain to me why when adding the key/value pair to a hash in this case is movies[title] = rating? I originally did movies[title, rating] but it didn't work.

Following on from that can I input a value to an existing key?

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


movies = {
    'Harry Potter' => 5,
}

puts 'What is your favourite movie?'

choice = gets.chomp

case choice
when "add"
    puts "Please input the name of the movie."
    title = gets.chomp
    puts "What is your rating on this movie? 0 worst and 5 best."
    rating = gets.chomp
    movies[title] = rating
when "update"
    puts "Updated!"
when "display"
    puts "Movies!"
when "delete"
    puts "Deleted!"
else
    puts "Error!"
end


#2

This is the prompt for choice, not the movie name.

When using index subscript notation, there can only be one index.

myHash = { title: 4 }

A hash is made up of key-value pairs. In this exercise, the key is title and the value is rating.

puts myHash[title]    # 4

Be sure to read the instructions carefully, and follow them as closely as possible. Look to the first lesson for an example if you get stuck.


#3

Thanks for your input!

Oops sorry I didn't realise I hadn't changed that prompt question as I'm doing the Ruby course again for the 3rd time :smiley:

This is my finished code:

movies = {
    Harrypotter: 5,
    Cinderella: 3,
    Batman: 3,
    Spiderman: 4,
}

puts "Please enter \n add (to add a movie) \n update (to update movie) \n display (to display movies) \n delete (to delete a movie)"

choice = gets.chomp

case choice
when "add"
    puts "Please enter the movie title (do not add any spaces): "
    title = gets.chomp
    title.capitalize!
    if movies[title.to_sym].nil?
        puts "Please give the movie a rating (0 = worst, 5 = best): "    
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
        puts "#{title} has been added with a rating of #{rating}"
    else
        puts "The movie already exists with a rating of #{movies[title.to_sym]}."
    end 
when "update"
    puts "Please enter the movie title: "
    title = gets.chomp
    title.capitalize!
    if movies[title.to_sym].nil?
        puts "This movie is not on the database! Please add this movie instead."
    else
        puts "Please enter new rating for this movie: "
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
    end
when "display"
    movies.each do |movie, rating|
        puts "#{movie}: #{rating}"
    end
when "delete"
    puts "Please enter the movie you would like to delete: "
    title = gets.chomp
    title.capitalize!
    if movies[title.to_sym].nil?
        puts "This movie is not in the databse!"
    else
        movies.delete(title)
        puts "#{title} has been deleted."
    end
else
    puts "Error!"
end

I think I understand this much better now! Thanks :smile:


#4

Personally, I favor not manipulating the input. Make the user input the correct value with respect to proper names.

Why not? How many movies have more than one word in the title? Ruby can handle multi-word inputs in keys.

"Motorcycle Diaries"

works as a text key, and converts readily to a symbol.

If you root around in this forum there were a couple of discussions about symbols and snake_case as the medium in the hash. I had some of my own opinions in those discussions, but that's not to say I gave the best advice, just presented ideas. I actually contradict what I just said above in this one:

https://discuss.codecademy.com/t/7-update/53870/7

We still have the option to preserve the extra input and neither downcase nor upcase during conversion.

This one may shed some more light on customization...

https://discuss.codecademy.com/t/extra-step-for-fun-force-user-choice/36680

All food for thought.


#5

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