4. Prompting: Redux!


#1

I know something is incomplete with my code and I receive this error message,

"Oops, try again. It looks like you didn't add the princess bride to the movies hash!"

I've tried several different variations on the hint, but none of those seem to write to the movie hash either.

movies = {
  While You Were Sleeping: 3,
  The Princess Bride: 4,
  The Ring: 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."
puts "input the option : "
choice = gets.chomp.downcase

case choice
when "add"
    puts "Movie title?"
    title = gets.chomp.downcase
    puts "Movie rating?"
    rating = gets.chomp.downcase
when "update"
    puts "Updated!"
when "display"
    puts "Movies!"
when "delete"
    puts "Deleted!"
else
    puts "Error!"
end


#2

[quote="sarahmcr, post:1, topic:53841"]
Correction:
movies = {
While_You_Were_Sleeping: 3,
The_Princess_Bride: 4,
The_Ring: 1
}


#3

nowhere in your code do you access the hash to push your information to or from it. i could show an example if you need me to just let me know


#4

An example would be great. Thanks!


#5

you'll first need to convert your input string to symbol for title and to an integer for the rating. you do this by using

 to_sym

and

to_i

so you would want

title.to_sym!

and

rating.to_i

to add a value to a hash you would use the hash name[key] = value so if i had a hash of restaurants names and ratings i would do

restaurant[name.to_sym] = rating.to_i

this will add an item to the hash


#6

I added some of the code as I've interpreted your suggestion, @cfecteau1 , but am still receiving an error: "Oops, try again. It looks like you didn't add movie to the movies hash!"

movies = {
While_You_Were_Sleeping: 3,
The_Princess_Bride: 4,
The_Ring: 1
}
puts "What would you like to do?"
puts "- 'Add' to add a movie."
puts "- 'Update' to update a movie."
puts "- 'Display' to display all movies."
puts "- 'Delete' to delete a movie."
puts "input the option : "
choice = gets.chomp.downcase

case choice
when "add"
    puts "Movie title?"
    title = gets.chomp.downcase
    puts "Movie rating?"
    rating = gets.chomp.downcase
    if movies[title.to_sym].nil?
    
    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 "Updated!"
when "display"
    puts "Movies!"
when "delete"
    puts "Deleted!"
else
    puts "Error!"
end

`


#7

@cfecteau1Never mind, I had too much detail. I deleted the end and it worked.


#8

sorry just looked at the exercise, for this one remove the to_sym and to_i then you will pass. It is technically correct just not for their test and instructions. you'll see the to_sym and to_i in the next exercise.


#9

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