Update


#1



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


console: what would you like to do?
update
what is the title?
split
what is the rating?
4
split has been added with a rating of 4.
nil

Oops, try again. Make sure to test your program by choosing 'update'.

movies = {
   Harry potter: 4
}
puts "what would you like to do?"
choice = gets.chomp
choice = case
when "add"
puts "what is the title?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "what is the rating?"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been added with a rating of #{rating}."
  else
    puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
end
when "update"
puts "What movie do you want to update?"
title=gets.chomp
if movies[title.to_sym].nil?
    puts "Movie not found!"
else
    puts "What's the new rating? (Type a number 0 to 4.)"
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
    puts "#{title}has been updated with new rating of #{rating}."
end
when "display"
puts "Movies!"
when "delete"
puts "Deleted"
else 
    puts "Error!"
end


#2

You're overwriting the user input so that when it checks the variable that was supposed to contain it, something else was there instead


#3

not sure what you mean.


#4

You store the user input in a variable

but later you assign that variable to something else so it is no longer referring to 'update'


#5

movies = {
   Amelie: 4
}
puts "what would you like to do?"
choice = gets.chomp
choice = case
when "add"
puts "what is the title?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "what is the rating?"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been added with a rating of #{rating}."
  else
    puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
end
when "update"
puts "What movie do you want to update?"
title=gets.chomp
if movies[title.to_sym].nil?
    puts "Movie not found!"
else
    puts "What's the new rating? (Type a number 0 to 4.)"
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
    puts "#{title}has been updated with new rating of #{rating}."
end
when "display"
puts "Movies!"
when "delete"
puts "Deleted"
else 
    puts "Error!"
end

im not sure where in the code you mean i assigned update to something else

console:
what would you like to do?
update
what is the title?
wow
what is the rating?
4
wow has been added with a rating of 4.
nil

error: Oops, try again. Make sure to test your program by choosing 'update'.


#6

update isn't a variable in your program, but choice is
choice should refer to the string 'update' because that's what the user's choice is (assuming that's the text they entered)
look at your code, where do you assign to it? (the assignment operator is =)


#7

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