4. Prompting: Redux


#1

Ok so I normally dont have much of a problem figuring out where my issues are, but I just dont get this one. Here is the code i have

movies= {
'The King and I' => '4'
}
puts "What would you like to do?"
movies= gets.chomp
case movies
when "add"
puts "What is your favorite movie?"
title = gets.chomp

puts "What's the rating? (Type a number 0 to 4.)"
rating = gets.chomp
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

I keep getting the following error:

Oops, try again. It looks like you didn't add The King & I to the movies hash!

even though in the console it clearly states:

What would you like to do?
add
What is your favorite movie?
The King & I
What's the rating? (Type a number 0 to 4.)
4
The King & I has been added with a rating of 4.
nil

please help me figure out where I went wrong


#2

Hi. I think it only prints that its been added since you cause it to print with your latter puts command.

change:
movies = get.chomp
case movies
to:
choice = gets.chomp
case choice

Also, add:
movies[title] = rating
after
rating = gets.chomp

Thus, this worked for me, using your code:
movies= {
'The King and I' => '4'
}
puts "What would you like to do?"
choice = gets.chomp
case choice

when 'add'
puts "What is your favorite movie?"
title = gets.chomp
puts "What's the rating? (Type a number 0 to 4.)"
rating = gets.chomp
movies[title] = rating

when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end


Prompting: Redux
#3

Why does
movies[titles]=rating
work? That doesn't make any sense to me. Please help.


#4

movies[titles]=rating

This doesn't make sense to me either. Can someone help explaining, please...?


#5

So it worked, but I don't understand why it works, or why movies[title] = rating needs to be added... any explanation would be great


#6

Yeah I don't understand why this works either how does the movies Hash know to put the title and the rating variables into the hash with the command movies[title] = rating ?


#7

movies[title] = rating works because your storing a value(rating) for the movie title.

[title] = rating
Stepbrothers = 10

movies = {
"Step Brothers" => 10
}


#8

I tried to compare my code to everything I found on the web site in these discussions, but I still get the same "Error" and am completely stuck with the code. Probably I wasnt attentive enough but still cant find the mistake.If you had a look at it and helped me in analysing it, I would really really really be grateful.

movies = {
'Hotel Marygold' => 4,
'Dallas Buyer Club' => 5,
}
puts "Select your movie."
choice = gets.chomp.downcase

case choice
when 'add'
puts "Type the title please"
title = gets.chomp
puts "What is the rating? (Enter 0 - 5)"
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


#9

Hi here where you have movie[title] = rating
Try to put rating in " "


#10

Thanks for the tipp! But it didnt work. Ruby says there is nothing added to the movie hash


#11

Hi try to change it like that

movies['title'] = rating

#12

didnt work either

gives the same Error out


#13

When you Save & submite the code in the output you should first enter add and then the movie you want and also the rating


#14

That`s it! Thanks sooo much!


#15

-delete puts "Added!", and use the following

    puts "Which movie to add?"
        title=gets.chomp
    puts "What is the rating?"
        rating=gets.chomp
    puts "Title and the rating of the movie has been added"
movies [title]=rating