If/Else Statements Not Working


#1

Hello! I have been googling, scouring forums, and otherwise pulling my hair out trying to figure out why my if/else statement is not working for the Night at the Movies project. Ruby let’s me advance in the activity, but knowing that my code doesn’t work feels like cheating.

The console only displays the IF statement, even when I provide a name that is already on the list.

movies = {
Thor: 3,
The_Hunt_For_The_Wilderpeople: 4,
What_We_Do_In_The_Shadows: 3,
Dune: 1
}

puts "What would you like to do? "
puts “Type ‘add’ to add your own movie to the list.”
puts “Type ‘delete’ to delete one of these movies from the list.”
puts “Type ‘update’ to update a rating on this list.”
puts “Type ‘display’ to see all the movies on this list.”

choice = gets.chomp.downcase
case choice
when ‘add’
puts "What movie would you like to add? "
title = gets.chomp.upcase
if movies[title.to_sym.upcase].nil?
puts "What is the rating for this movie? (Type a number 0 to 4) "
rating = gets.chomp
movies[title.to_sym.upcase] = rating.to_i
puts “Your movie #{title} has been successfully added to our list with a rating of #{rating}!”
else
puts “That movie is already on the list!”
end

when ‘update’
puts “What is the title you’d like to update?”
title=gets.chomp.upcase
if movies[title.to_sym].nil?
puts “This movie does not exist. Please select ‘add’ to add this movie.”
else
puts “What is the rating for your movie?”
rating= gets.chomp
movies[title.to_sym]=ratings.to_i
puts “#{title} has been updated with a rating of #{rating}.”
end

when ‘display’
puts “Movies!”
when ‘delete’
puts “Deleted!”
else
puts “Error!”
end


#2
movies = {
  Thor: 3,
  The_Hunt_For_The_Wilderpeople: 4,
  What_We_Do_In_The_Shadows: 3,
  Dune: 1
  }
 > 
 What would you like to do? 
 Type 'add' to add your own movie to the list.
 Type 'delete' to delete one of these movies from the list.
 Type 'update' to update a rating on this list.
 Type 'display' to see all the movies on this list.
  add
 What movie would you like to add? 
  Thor
 What is the rating for this movie? (Type a number 0 to 4) 
  3
 Your movie THOR has been successfully added to our list with a rating of 3!
=> nil
 > movies
 > 
=> {:Thor=>3, :The_Hunt_For_The_Wilderpeople=>4, :What_We_Do_In_The_Shadows=>3, :Dune=>1, :THOR=>3}
   

It would appear your inputs are over manipulated.

gets.chomp.upcase

#3

Thank you. So, in this case, would I remove the .upcase on the gets.chomp? I’d added it so that Ruby would read the variable the same as capitalized and recognize it as the same title? OR, do I need to use the older version of symbol notation, with the hash rocket?


#4

Yes, since we do not wish to modify the inputs, only convert them to symbols when storing. That way their original case (namely, capitalization) is preserved.

 title = get.chomp    #  Thor
 if movies[title.to_sym].nil?
     #  add movie
 else
     #  title already in database
 end

Your hash can be written with the newer syntax after the first couple of lessons.

 Thor:

However, Ruby will still see the old syntax internally. The print out above is from Ruby 2,5 on REPL.IT.

The following is from lesson 10 of the unit:

your command: 
add
Movie title: 
A Night at the Movies
Movie rating: 
3
A Night at the Movies added with rating 3.
{:Crash=>4, :Babel=>4, :"A Night at the Movies"=>3}

Note the syntax below, (modern symbol syntax)

movies = {
  Crash: 4,
  Babel: 4,
  "A Night to Remember": 2
}

and how Ruby displays it…

your command: 
display
Crash: 4
Babel: 4
A Night to Remember: 2
puts movies
{:Crash=>4, :Babel=>4, :"A Night to Remember"=>2}

Something of note:

Write your original values without underscores…

movies = {
  Thor: 3,
  "The Hunt For The Wilder People": 4,
  "What We Do In the Shadows": 3,
  Dune: 1
  }

#5

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