4/10 Promoting: Redux! // Oops!


#1



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


The code itself seem to be correct as after changing it several times according to an error messages it wouldn't run the code along with following: "Oops, try again. It looks like you didn't add to the movies hash!" message.

I've tried different browsers and even another computer with no luck.

Is there a mistake in a code I do not see?


movies = {
 Gremlins: 7,
 Tarzan: 7
}
puts "What would you like to do (Add, Update, Display, Delete)"

choice = gets.chomp

case choice
when "add"
    puts "Enter Movie Title"
    title = gets.chomp
    puts "Enter a rating!"
    rating = gets.chomp
    movies[title] = rating
    puts "Movie has been added!"
when "update"
    puts  "Updated!"
when "display"
    puts "Movies!"
when "delete"
    puts "Deleted!"
else
    puts "Error!"
end

Any suggestions?


#2

Have you investigated whether they are added as that's what your error message is about?


#3

They are added as far as I'm concerned. The trouble is that I am not aware of further steps in order to work the problem in this kind of situation.

Would you advice any particular action to be implemented?


#4

Are they?

For example, if you don't run your code, then it will not be adding anything to any hashes.

If you run half your code, then half of what your code describes will be executed.

Are you executing code that adds hashes to your movies?


#5

I gave another few tries running the upper part of the code that must include hashes to movies however I don't seem to be finding different angles on the problem.

I might need to ask you to expand the question as for a 5 year old, please. :disappointed_relieved:


#6

If you don't select your add option, then you won't be adding.
Find out if you're adding to your hash or not! Guessing won't do. So, you would for example count the number of entries, then do the supposed adding, then count them again. Of course, if you're simply not telling it to add, then you would need to start there as otherwise there is no reason to believe that it would be doing any adding.


#7

It's clear that in order to make it work I must type a line of code after an "add" statement to add movie I have typed into hash movies. I seem to be jumping around not finding a solution as well as wasting time.

puts "'#{title}' has been added with a rating of '#{rating}'" ?

Could you give me an example?


#8

The submission test checks if you added an entry to movies with the value that title refers to as the key.

That only happens if you run the program and select your "add" option. If you don't do that, then you aren't running the code that you wrote and you indeed will not have added anything to the hash.

You're not adding an entry to the movies --> do the actions that cause it to be added

What would you like to do (Add, Update, Display, Delete)
 add
Enter Movie Title
 bob eats dinner
Enter a rating!
 9001
Movie has been added!

#9

It seems to be way too obvious!

I give up, I cannot spend any more time on it. I followed your instruction It's still tells me that I am not adding to hash.


#10

In your code, there is a condition for adding to your hash
If you do not satisfy it by selecting the add option, then nothing is added.

Your code's behaviour relies on your user input. You have to provide the user input that causes the action that the error message says isn't happening, happen.


#11

It makes perfect sense. I am very stuck. What is the condition you mentioned?

How do I add the action?

I'm pretty sure I know it but after two hours of this piece code I'm having a massive blank.


#12

That's the condition, the user has to choose the add action, otherwise that code doesn't run


#13

movies = {
Gremlins: 7,
Tarzan: 7
}
puts "What would you like to do (Add, Update, Display, Delete)"
choice = gets.chomp

case choice
when "add"
puts "Enter Movie Title"
title = gets.chomp
puts "Enter a rating!"
rating = gets.chomp
movies[title] = rating
puts "#{title} has been added with a rating of #{rating}."
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end

What else can I do here?


#14

What input is the user of your program required to enter in order to cause your program to add to the hash?

You have to give your program that input. Otherwise, it does not add to the hash.

Don't change the code. Run your program and tell your program to add to the hash.


#15

IT WORKED! Yes!

Thank you very much for your help!!


#16

:medal:


#17

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