Error in data stored


#1

No matter what title/rating you enter, my code only adds, updates or deletes themartian: 4, which is the first pair I added when first tested my "add" block. Other than that, "The Martian", 4 is not anywhere in my code.

movies = { jumanji: 2 }

def get_title()
    puts "Please, provide movie title."
    title = gets.chomp.downcase.to_sym
end

def get_rating()
    puts "Please, provide rating."
    rating = gets.chomp.to_i
end

choice = ""

until choice == "end" do

    puts "Add, update, display or detele a movie?"
    choice = gets.chomp.downcase
    
    case choice
    when "add"
        get_title()
        if movies[title].nil?
            get_rating()
            movies[title] = rating
            puts "#{title} has been added with a rating of #{rating}"
        else
            puts "#{title} has already been rated"
        end
    when "update"
        get_title()
        if movies[title].nil?
            puts "Sorry, #{title} is not in our records. You can add the movie or try again with a new name"
        else
            #modify movie
            get_rating()
            puts "#{title} has been updated with a rating of #{rating}"
        end
    when "display"
        movies.each {|movie,rating| puts "#{movie}: #{rating}"}
    when "delete"
        get_title()
        if movies[title].nil?
            puts "#{title} is not in our records"
        else
            movies.delete(title)
            puts "#{title} has been deleted"
        end
    when "end"
        puts "Bye"
    else
        puts "Error!"
    end

end

#2

Add, update, display or detele a movie?
add
Please, provide movie title.
bob eats dinner
movies.rb:23:in `<main>': undefined local variable or method `title' for main:Object (NameError)

Perhaps it's got something to do with using a variable that you haven't defined - except you had probably given it the value "The Martian" at some earlier point so it was defined


#3

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