7. Update / Code can pass but does not actually function correctly


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-0i8v1/0/7?curriculum_id=5059f8619189a5000201fbcb#


The code I have input will let me pass to the next lesson, however the output does not function properly.

Under 'update', when I try to input a movie that already exists, i get back my prompt "Oops this movie doesn't exist." Codecademy accepts this though and lets me move on.

I've re-tested 'add' as well, and even if the movie i add already exists, it still takes the full input as if it was new.

I want to make sure I'm doing this right, even if i can go on to the next lesson.

My code is below


movies = {
    "the birdcage" => 5,
    "iron man" => 1,
}
puts "What would you like to do?"
puts "--add a movie."
puts "--update a rating."
puts "--display movies."
puts "--delete a movie."
choice=gets.chomp
case choice
when 'add'
    puts "What's the movie called?"
    title=gets.chomp
    if movies[title.to_sym].nil?
        puts "What do you rate this move 1 - 5?"
        rating=gets.chomp
        movies[title.to_sym]=rating.to_i
        puts "Movie added!"
    else
        puts "Movie already exists!"
    end
when 'update'
    puts "What movie do you want to update?"
    title=gets.chomp
    if movies[title.to_sym].nil?
        puts "Oops! This movie does not exist!"
    else
        puts "What's the new rating?"
        rating=gets.chomp
        rating=rating.to_i
        movies[title.to_sym]=rating.to_i
        puts "Great! It's been updated!"
    end
when 'display'
    puts "Movies!"
when'delete'
    puts "Deleted!"
else
    puts "Error!"
end


#2

This line is not needed and may be removed.

There is nothing that I can see wrong with your code. It would be nice to see the operators = so a little white space around them is in order. Apart from that you are on the right track and should feel okay progressing to the next lesson.

There is no cycling in this program, even once we get to the finish of the module. It only runs once through. As long as the output is what you expect, be happy.


#3

hey, i'm having the same problem! my code pretty much matches yours & the sample code, but if i try to update a pre-existing movie's rating, i'm told that movie isn't in the database (and i'm using the one movie that is in the database, spelled correctly, etc., in order to test this code).

movies = {
    Annie_Hall: 3,
    Jurassic_Park: 3
}
puts "What would you like to do? Type ADD, UPDATE, DISPLAY, or DELETE: "
choice = gets.chomp

case choice
when "add"
        puts "Movie title: "
        title = gets.chomp
    if movies[title.to_sym].nil?
        puts "Movie rating (scale of 0-4): "
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
        puts "Database updated!"
    else 
        puts "Whoops — that movie is already in the database!"
    end   
when "update"
        puts "Movie title: "
        title = gets.chomp
    if movies[title.to_sym].nil?
        puts "That movie isn't in the database yet!"
    else 
        puts "Updated rating (scale of 0-4): "
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i   
        puts "Database updated!"
    end
when "display"
    movies.each do |title, rating|
        puts "#{title}: #{rating}"
    end
when "delete"
    puts "Deleted!"
else
    puts "Error!"
end

#4

If the movies in your database are not symbols. the program will not find them since it is looking for symbols, not strings.

movies = {
    Babel: 4,
    Crash: 4,
    "Motorcycle Diaries" => 4
}

#5

Mine are listed as symbols :confused:


#6

Please post a link to this lesson, and a copy of your movies hash. Thank you.

Ignore that. The problem might be with the way you are writing the names. Normal names are capitalized with white space, not underscores. When asked for a title, we would normally enter it as it is expected:

Annie Hall
Jurassic Park

which in symbol form, are,

"Annie Hall" => 3,
"Jurassic Park" => 3

#7

A post was split to a new topic: My code doesn't work propoerly but I can pass the step