A Night at the Movies loop


#1

Hi, I have been trying to play with this code to make it so that when you finish adding a movie it will ask you what you want to do again and I can't seem to make it work. The best I can do is something like this:

movies = {
Memento: 3,
Primer: 4,
Ishtar: 1
}
def intro
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."
end

intro

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

It doesn't allow me to have more user input. Thanks for the help


#2

What's stopping you from putting all user-interaction code in a loop?


#3

You mean putting the choice in the intro or making each choice (add, display etc) its own loop or making it just one long loop?


#4

loop do
  all the things
end

#5

Thanks! Didn't think about that. That was really helpful.


#6

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