Dealing with multi-word titles/Repeating program


#1


I've successfully completed the Night at the Movies challenge, but only by using one-word movie titles! I'm trying to figure out a way to do multi-word movie titles without the program messing up. Any ideas? Is it possible to have multi-word keys, or multi-word symbols?

ANOTHER QUESTION: When dealing with "case", is there a good way to repeat the program so after you do an action (update, delete, etc) it will ask you what you want to do next?

My current code, which works just fine with single-word titles.

movies = {
    Labyrinth: 3,
    Cinderella: 4,
    Balto: 2
}

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 would you like to add?"
    title = gets.chomp.downcase.capitalize
    if movies[title.to_sym].nil?
        puts "What do you rate this movie on a scale from 0 to 4?"
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
    puts "Added!"
    else
        puts "That movie has already been added!"
    end

when 'update'
    puts "What movie do you want to update?"
    title = gets.chomp.downcase.capitalize
    if movies[title.to_sym].nil?
        puts "This movie has not been added yet."
    else
        puts "What is the new rating?"
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
        puts "#{title}'s rating has been updated to #{rating}."
    end

when "display"
movies.each {|movie, rating|
puts "#{movie}: #{rating}"
}

when "delete"
puts "What movie do you want to delete?"
title = gets.chomp.downcase.capitalize
if movies[title.to_sym].nil?
    puts "Movie not found."
else
    movies.delete(title.to_sym)
    puts "Movie deleted!"
end

else
    puts "Error!"
end


#2

Update: I should have googled first. These were easy changes!

movies = {
    Labyrinth: 3,
    Cinderella: 4,
    Balto: 2,
    Blues_brothers: 4
}

choice=[]
until choice == "exit" do

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 "-- Type 'exit' to exit the program."

choice = gets.chomp.downcase
case choice
when 'add'
    puts "What movie would you like to add?"
    title = gets.chomp.downcase.capitalize
    title.gsub!(/ /, "_")
    if movies[title.to_sym].nil?
        puts "What do you rate this movie on a scale from 0 to 4?"
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
    puts "Added!"
    else
        puts "That movie has already been added!"
    end

when 'update'
    puts "What movie do you want to update?"
    title = gets.chomp.downcase.capitalize
    title.gsub!(/ /, "_")
    if movies[title.to_sym].nil?
        puts "This movie has not been added yet."
    else
        puts "What is the new rating?"
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
        puts "#{title}'s rating has been updated to #{rating}."
    end

when "display"
movies.each {|movie, rating|
puts "#{movie}: #{rating}"
}

when "delete"
puts "What movie do you want to delete?"
title = gets.chomp.downcase.capitalize
title.gsub!(/ /, "_")
if movies[title.to_sym].nil?
    puts "Movie not found."
else
    movies.delete(title.to_sym)
    puts "Movie deleted!"
end
 when "exit"
 puts "Goodbye!"


else
    puts "Error!"
end
end

9. Delete!
#3

If we are not manually writing the literal keys, the above line is how to allow whitespace.

Pretend the following are user input from gets.chomp

title = "Motorcycle Diaries"
rating = "4"

# add to data hash
movies[title.to_sym] = rating.to_i

Mutating the user input to snake case should be done by the program, if at all, and the program should be able to switch it back to Title Case (proper name) when it is searched for. Personally, I would never manually write snake_case in a hash. Quoted proper names can be easily converted to symbols. That should be sufficient.


#4

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