I don't understand why this loops?

https://www.codecademy.com/courses/learn-ruby/lessons/a-night-at-the-movies/exercises/delete

So I’m going through this course for the second time to try and make sure I understand it all.
I’ve refactored this to have a little less repetition (and get used to using def methods, saving values from them etc. and whilst my code isn’t dry, it is drier).

  testy: 5,
  the_room: 4,
  shes_the_man: 3,
  love_wedding_repeat: 0,
  the_wicker_man: 2,
}

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

def title
  puts "Title please?"
  gets.chomp.to_sym
end
def rating
  puts "What rating would you like that title to be?"
  gets.chomp.to_i
end
def exist?(inputted_title, movie_hash)
  movie_hash[inputted_title] != nil ? true : false
end

case choice
when "add"
    title_input = title
    rating_input = rating
      if exist?(title_input, movies)
        puts "That movie already exists."
      else
        movies[title_input] = rating_input
      puts "Your movie has been added"      
      end
    
  when "update"
    title_input = title
    rating_input = rating
      if exist?(title_input, movies)
        movies[title_input] = rating_input
        else
        puts "That movie does not exist" 
      end
  when "display"
    movies.each {|k, v| puts "#{k}: #{v}"}
  when "delete"
    title_input = title
    if exist?(title_input, movies)
      movies.delete(title_input)
      puts "Movie entry has been removed."
    else
      puts "No such entry exists, unable to delete non-existant entry"
    end
end
p movies```

When I do some of the cases, take delete for example, it gets into a loop just going "title please?" I input a title, it should move on but then it accepts that title and asks again "title please?"

Is it a problem with my code or is it a problem with the interface? (I know ruby is meant to be hard to replicate in a browser, I don't know why but I do accept that).

Thanks for any and all help :)

Hello @bit6397009343. I think the problem might be with this (and others like it):

Wouldn’t you need to store gets.chomp#other stuff to a variable?

puts ">>"
answer = gets.chomp#stuff

I hope this helps!

That should be stored in the title_input variables, I’d ideally only have one of those but that can only run once per program because of the case (plan to try and reformat a more efficent code once all the exercises have been completed) which is basically a huge if statement, it can’t function like that (yet).

1 Like

Oddly now the code and console are working together fine, so I’m going to assume my code is fine and that the issue is with the web console.

Thanks for your help :slight_smile:

1 Like

I have finished my project, which was to do:

https://www.codecademy.com/courses/learn-ruby/lessons/a-night-at-the-movies/exercises/nice-work-1

With dry code, and only accepting valid inputs.

movies = {
  testy: 5,
  the_room: 4,
  shes_the_man: 3,
  love_wedding_repeat: 0,
  the_wicker_man: 2,
}

def user_input
	loop do
 		puts "What would you like to do? \n add \n update \n delete \n display"
    valid = gets.chomp.downcase
    #valid.downcase!
  return valid if ["update", "add", "display", "delete"].include?(valid)
  end
end

def title
  puts "Title please?"
  gets.chomp.to_sym
end
def rating
  puts "What rating would you like that title to be?"
  gets.chomp.to_i
end
def exist?(inputted_title, movie_hash)
  movie_hash[inputted_title] != nil ? true : false
end

choice = user_input
case choice
  when "display"
     movies.each {|k, v| "#{k}: #{v}"}
  else
    title_input = title
    case choice
      when "delete"
        if exist?(title_input, movies)
          puts "Movie entry has been removed."
          movies.delete(title_input)
        end
      else
      
    case choice
      when "add"
        if exist?(title_input, movies)
          puts "That movie already exists."
        else
          rating_input = rating
          movies[title_input] = rating_input
          puts "Your movie has been added"  
      end
      when "update"
        if exist?(title_input, movies)
          rating_input = rating
          movies[title_input] = rating_input
        else
          puts "That movie does not exist" 
      end
        else
          puts "No such entry exists, unable to delete non-existant entry"
        end
      end
end
p movies

I actually think it’d be a really good challenge to add in to the course provided they give a few reading materials more into things like the return statement and how to if statement multiple things at once (like: if x == a || b || c should actually be if x == a || x == b || x == c)

1 Like