Prompting: Redux!


#1

Hi, i have an error Oops, try again. It looks like you didn't add ty to the movies hash!. I even copied the example code and still error!

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

#2

Hi can you post your code?


#3

I posted it in first message. I try sumbit this code. Anyway error

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
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 movies
  else
    puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
  end
end

#4

I don't understand, what's wrong with my decision?? I have thise code:

movies = {
Metro: 1,
Matrix: 0,
oslo: 3
}

puts "What would you like to do?"
choice = gets.chomp

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}: #{rating}"
  else
    puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
  end
end

puts movies

After execution, I have this in console:
What would you like to do?
add
What movie do you want to add?
Titan
What's the rating? (Type a number 0 to 4.)
2
Titan: 2
{:Metro=>1, :Matrix=>0, :oslo=>3, :Titan=>2}
nil


#5

On Lesson 3 they aske you to make a case statement somethig like that

case choice
    when "add"
        puts "Added!"
    when "update"
        puts "Updated!"
    when "display"
        puts "Movies!"
    when "delete"
        puts "Deleted!"
    else
        puts "Error!"
end

then in Lesson : Prompting: Redux! they akse you...

1 Inside your when "add" block, remove the puts "Added!" statement.
2 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"
    print "What movie do you want to add?"
    title = gets.chomp

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

when "add"
    print "What movie do you want to add?"
    title = gets.chomp 
    print "What's rating do you give to that Movie?"
    rating = gets.chomp

4 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"
    print "What movie do you want to add?"
    title = gets.chomp 
    print "What's rating do you give to that Movie?"
    rating = gets.chomp
    movies[title] = rating
    puts "A movie with the title #{title} was add with the rating of #{rating}"

#6

I am very grateful for your detailed answer. I spent almost 2 hours trying to resolve it.. Merci.