6. Error! Error!


#1

I cannot for the life of me figure out what is going on.

When I try to add a movie that already exists, I get the error message defined by else ""#{title} already exists. Its rating is #{rating}!" The only issue is the #{rating} gives me the number 1, when it should be 4.

Here is my code starting from the movies hash and ending with me add section:

movies = {
"Memento" => 4,
"Inception" => 3,
"The Prestige" => 2,
"Interstellar" => 1
}

puts "What would you like to do?"
choice = gets.chomp.downcase

case choice

when "add"
puts "What would you like to add?"
title = gets.chomp
if movies[title].nil?
puts "What its rating? (enter 1-4)"
rating = gets.chomp
puts "#{title} has been added with a rating of #{rating}"
rating = movies[title]
else puts "#{title} already exists. Its rating is #{rating}!"
end


#2

I didn't have symbols in my movies hash. Here is the code that ended up working!

when "add"
puts "What would you like to add?"
title = gets.chomp
title = title.to_sym
if movies[title.to_sym].nil?
puts "What its rating? (enter 1-4)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been added with a rating of #{rating}"
else puts "#{title} already exists. Its rating is #{movies[title.to_sym]}!"
end


#3

Just to throw my two cents in, and because I feel like I've kept my code fairly simple througout this exercise, here is what I have so far that got me a pass on this excercise. I'm still trying to fully wrap my head around all of these concepts, but I've been practicing daily and I'm feeling pretty good about where I'm at. Please feel free to critique!. Cheers, mates!
movies = {
Memento: 3
}
puts "What is your favorite movie?"
choice = gets.chomp

case choice
when "add"
puts "Please enter a movie title."
title = gets.chomp
if movies[title.to_sym] = nil
puts "What rating will you give it? (pick number 1-5)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "The movie #{title} has been added with a rating of #{rating}."
else
puts "That movie already exists!"
end
when"update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end


#5

movies = {dogville:4
}
puts"What do you want to do?"
choice=gets.chomp
case choice
when "add"
puts "Which movie to add?"
title=gets.chomp
puts "What is the rating?"
rating=gets.chomp
if movies[title.to_sym].nil?
movies[title.to_sym]=rating.to_i
puts "Title and the rating of the movie has been added"
else
"Title and the rating of the movie has already existed"
end
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else puts "Error!"
end


#6

if movies[title.to_sym].nil? is working with your code. and if movies[title.to_sym] = nil doesn't.


#7

Hello, everyone. Below is the code I used, after about a million attempts (thanks to some tips from this thread too), to get past the gatekeeper:

movies = {
"First Blood" => 3,
"Undercover Brother" => 4,
"Django Unchained" => 4,
}

1 puts "Type 'add' to add movie."
2 puts "Type 'update' to update."
3 puts "Type 'display' to display movies."
4 puts "Type 'delete' to remove movie."
5 choice = gets.chomp

6 case choice
7 when "add"
8 puts "Type in A New Movie Name"
9 title = gets.chomp
10 puts "What rating will you give your movie?"
11 rating = gets.chomp
12 if movies[title.to_sym].nil?
13 movies["#{title.to_sym}"] = rating.to_i #...Note this line in particular. I succeeded by using the hash and quotes...
14 puts movies
15 puts "\n\vYour new movie and its rating have been added!"
end

17 when "update"
18 puts "Updated!"
19 when "display"
puts "Movies!"
20 when "delete"
21 puts "Deleted!"
22 else
23 puts "Error!"
24 end

Please carefully note line 13. Writing it that exact way may be the key that finally unlocks the dungeon for you, so try it. Do I understand it? Heck, no! But I played around and somehow got it.

PS: LIne 15 contains some \n and \v. You don't need it; I just learnt that way to put a line break and add some line spacing. Feel free to leave it out.:slight_smile:


#8

I don't get it.
I've been trying your lines and it gets me pass as well.
But I don't understand that if you type down a completely different movie name from what you already have in your hash, I still get my else statement!


#9

Hi all,

i would like to understand why is that the the code inside the hash if i put like "title" => x doesn't work with the [title.to_sym] but if i remove the .to_sym it will work as expected from the exercise. I understand that Ruby reads the code in two different ways when we put "title" or title: but if we use title: we cannot write with spaces because it will make Ruby confused, so how to correct this situation (using spaces in the titles) and go further on the exercise using the .to_sym?

EDIT: Found the problem, on why it was not working. in line marked with an arrow i added the .to_sym when i only should add that if i'm using the ( title: x, ) instead of ( "title" => x, ) was this the problem ???

CODE:

Hash with movie titles

movies = {
"Gladiator" => 4,
"The Crow" => 4,
"Green Mile" => 5,
"TMNT" => 3,
"007 Casino Royale" => 3.5
}

Choices

puts "Which movie do you want to see?"
puts "Type add to add movies to our list."
puts "Type update to update our list."
puts "Type display to see our list."
puts "Type delete to delete."

choice = gets.chomp

Case system

case choice
when "add"
puts "Which movie title you wish to add?"
title = gets.chomp

if movies[title.to_sym].nil? <----

    puts "What is the rating of this movie? (Type a number from 0 to 5)"
    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! Has a rating of #{movies[titles.to_sym]}"
end

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


#10

Tried the same lines of code not going through yet


#11

movies = {
Titanic: 3,
Muchach: 4,
Elephant: 5
}
puts "What Movie would you love to watch?"
choice = gets.chomp

case choice

when "add"
puts "What movie would you want to add?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "What's the Rating? (Type a number 0 to 5.)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been added with a rating of #{arting}"
else
puts "That movie already exists! Has a rating of #movies[title.to_sym] "
end
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end


#12

Simplicity is beautiful (and the easiest to understand! :wink:).


#13

A post was split to a new topic: Is .nil? and = nil the same thing?


#14

I think I got stuck at line 13 as well. I can't figure out where I am doing wrong. syntax-wise , very modestly speaking there shouldn't be any error. I don't know how many more times I need to try :slight_smile: :frowning: :frowning:


#15

I was finally be able to solve the puzzle. what I don't understand is that in the first exercise/"demo code" , there was no <...#{...} around. like @systemsolver14429 's code 13 , i added hash and it worked.

It's still strange to me how this could happen. PLUS: while the code was supposed to check if the movie was already in there, it didn't.

i.e : I entered 'inception' , but it didn't return the statement is supposed to. Although it worked, that's another weird part!

====================== Below is the code worked for me ======================
movies = {
Memento: 3,
Inception: 4,
Gladiator: 4
}

puts "What would you like to do?"
puts "-- Type 'add' to add a movie."

choice = gets.chomp.downcase
case choice
when 'add'
puts "What movie do you want to add?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "What's the rating? (Type a number 0 to 4.)"
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