Updated code. Whats wrong with my code?


#1

https://www.codecademy.com/pt/courses/ruby-beginner-en-0i8v1/0/1?curriculum_id=5059f8619189a5000201fbcb

hi guys please check this code i was trying to update below. check the website("up") for the original code. so what im trying to do is i want the computer after "......
{...
...
....else
puts "Sorry, I didn't understand you. What yould you like to do ?"
choise2 = gets.chomp.downcase
end
end
}
I want the computer to ask the same question again. hencee i tried to make the whole thing a loop by making it a method by defining it without an agurment.
below is my

movies = {
  Memento: 3,
  Primer: 4,
  Ishtar: 1
}

puts "What would you like to do?"
puts "-- Type 'add' to add a movie."
puts "-- Type 'update' to update a movie."
puts "-- Type 'display' to display all movies."
puts "-- Type 'delete' to delete a movie."

choice = gets.chomp.downcase
case choice

def method
    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
    when 'update'
      puts "What movie do you want to update?"
      title = gets.chomp
      if movies[title.to_sym].nil?
        puts "Movie not found!"
      else
        puts "What's the new rating? (Type a number 0 to 4.)"
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
        puts "#{title} has been updated with new rating of #{rating}."
      end
    when 'display'
      movies.each do |movie, rating|
        puts "#{movie}: #{rating}"
      end
    when 'delete'
      puts "What movie do you want to delete?"
      title = gets.chomp
      if movies[title.to_sym].nil?
        puts "Movie not found!"
      else
        movies.delete(title.to_sym)
        puts "#{title} has been removed."
      end
    else
      puts "Sorry, I didn't understand you. What yould you like to do ?"
      choise2 = gets.chomp.downcase 
    end
end

method

so how do diplay the same gets.chomp ?


#2

There is no SCT for lesson 10, and looking back at what I had there was worth the visit:

def getstr(prompt, x="")
    loop do
        print prompt
        print "?"
        x = gets.chomp
        break if not x.to_s.empty?
    end
    x
end

def getnum(prompt, a=0, b=0, x=0)
    loop do
        x = getstr(prompt)
        break if (a..b).to_a.include? x.to_i
    end
    x
end

def not_found_error
    "<! Movie not found !>"
end
def exists_error
    "<! Movie already exists !>"
end
def prompt
  "< add | update | display | delete | quit >\n"
end
def found(t, r)
    "<% Found %>\n#{t}: #{r}"
end
movies = {
    Babel: 4,
    Crash: 4
}
loop do
  command = getstr(prompt).downcase
  break if command == 'quit'
  case command
  when "add"
    title = getstr("Enter a movie title")
    if movies[title.to_sym].nil?
      rating = getnum("Rate this movie (0..4)", 0, 4)
      movies[title.to_sym] = rating.to_i
      puts "#{title}: #{rating}"
    else
      puts exists_error
    end
  when "update"
    title = getstr("Enter a movie title")
    if movies[title.to_sym].nil?
      puts not_found_error
    else
      puts found(title, rating)
      rating = getnum("Rate this movie (0..4)", 0, 4)
      movies[title.to_sym] = rating.to_i
      puts "#{title}: #{rating}"
    end
  when "display"
    movies.each do |title,rating|
      puts "#{title}: #{rating}"
    end
  when "delete"
    title = getstr("Enter a movie title")
    if movies[title.to_sym].nil?
      puts not_found_error
    else
      puts found(title, rating)
      movies.delete(title)
      puts "#{title} deleted!"
    end
  else
    puts "<! Command not found !>"
  end
end
puts "Bye!"

There are a couple topics on symbols in this forum category that would be applicable here, as a further enhancement.


#3

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