10/10 undefined local variable or method


#1

So i whas trying to get this program to loop and once i got it working i suddenly had this problem:
* undefined local variable or method `movies' for # *

Here is my code:

$movies = {
Avatar: 10
}

again = true
while again do
database()
end

def database
puts "Would you like to ADD, UPDATE or DELETE a movie? or DISPLAY all movies?"
choice = gets.chomp
choice.downcase!
case choice

when "add"
puts "Movie title:"
title = gets.chomp
title.capitalize!
puts "Rate this movie 1-10"
rating = gets.chomp
if $movies[title.to_sym] == nil
$movies[title.to_sym] = rating.to_i
puts "Your movie has been added!"
else
puts "This movie is already in our list."
end

when "update"
puts "Movie title:"
title = gets.chomp
title.capitalize!
if $movies[title.to_sym] == nil
puts "This movie is not in our list."
else
puts "Rate this movie 1-10"
rating = gets.chomp
$movies[title.to_sym] = rating.to_i
end

when "delete"
puts "What movie do you want to delete?"
title = gets.chomp
title.capitalize!
if $movies[title.to_sym] == nil
puts "This movie is not in our list."
else
$movies.delete(title.to_sym)
puts "The movie #{title} has been removed"
end

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

else
puts "Your input whas not recognised"
end
end
puts "do you want to continue? yes / no"
input = gets.chomp
input.downcase!
if input == "yes"
again = true
else
again = false
end


#2

after some time trying different thing i found out the problem:

First of all the while loop shouldnt be above the method, the variable again should be global ($again)
and the end after "your input whas not recognised should be placed after the if/else statement.