Name of film with 2 words


#1

Hello ! I Need your help :slightly_smiling_face:!

You will find my code below:

movies = {
  forrest_gump: 5,
  the_beach: 2,
  inception: 5
  }

puts "Choose an action (add, update, display or delete): "
choice = gets.chomp.downcase

case choice
  when "add"
    puts "Add a film:"
    title = gets.chomp
    if movies[title.to_sym].nil?
	   puts "Rate the movie (1 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[title.to_sym]}."
    end
  when "update"
    puts "What is the movie you are looking for?"
    title = gets.chomp
    if movies[title.to_sym].nil?
      puts "This movie does not exist."
    else
      puts "What is your new rating (1 to 5)?"
      rating = gets.chomp
      movies[title.to_sym] = rating.to_i
    end
  when "display"
    movies.each do |movies, rating|
      puts "#{movies}: #{rating}"
    end
  when "delete"
    puts "What is the movie you need to delete?"
    title = gets.chomp
    if movies[title.to_sym].nil?
      puts "This movie does not exist."
    else
      movies.delete(title.to_sym)
    end
  else
    puts"Error!"
end

I have a problem with name of movie with several words. When I add a film that already exists, I should have an error message but I have this :

Choose an action (add, update, display or delete):
add
Add a film:
forrest gump
Rate the movie (1 to 5):

Why he does not recognize the name of the movie ?

Thank you.


#2

forest gump
forrest_gump


#3

Sorry, I make a mistake when I copied / pasted the code. The problem persists.


#4

that’s still not the same

There is no recognition happening. It’s an exact comparison. If it’s not the same it doesn’t match.


#5

What is the solution ?


#6

There’s not a single solution. And none of them require any programming knowledge, it’s text, you just have to decide what to do.
Change one or the other. Or change it when you read it.


#7

The supposed issue you’re having is that it doesn’t recognize the name of the movie…

It does! You’re putting in two different movie names.

Put more logically - “forrest gump” is not the same as “forrest_gump”… You’re trying to match two strings that are not equal, because of the underscore you’re using. Change one of the two inputs.


#8

The problem is in the hash syntax we can’t put spaces in the symbol. So when a user enters a name of the movie, he enters spaces when the name of the movie has multiple words.


#9

If you move the words around a bit in what you just said, then you did just say that symbols can contain spaces. (Ignoring the first sentence, which is wrong)

google:
ruby space symbol

Or use what you already used, because before comparing, you did create a symbol containing space. You could do the same in your code.


#10

Thank you!

It works with this synthax:

movies = {
  "forrest gump": 5,
  "the beach": 2,
  inception: 5
  }

puts "Choose an action (add, update, display or delete): "
choice = gets.chomp.downcase

case choice
  when "add"
    puts "Add a film:"
    title = gets.chomp
    if movies[title.to_sym].nil?
	   puts "Rate the movie (1 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[title.to_sym]}."
    end
  when "update"
    puts "What is the movie you are looking for?"
    title = gets.chomp
    if movies[title.to_sym].nil?
      puts "This movie does not exist."
    else
      puts "What is your new rating (1 to 5)?"
      rating = gets.chomp
      movies[title.to_sym] = rating.to_i
    end
  when "display"
    movies.each do |movies, rating|
      puts "#{movies}: #{rating}"
    end
  when "delete"
    puts "What is the movie you need to delete?"
    title = gets.chomp
    if movies[title.to_sym].nil?
      puts "This movie does not exist."
    else
      movies.delete(title.to_sym)
    end
  else
    puts"Error!"
end
    

#11

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