I'm really new to this but I’ve written a small program that will CRUD a movie to a list the only problem is it is not saving the changes made. how can I make this happen?
error = "movie not found"
movies = {
Mazerunner: 1
}
error = "movie not found"
puts "welcome to CrudMovies"
puts "enter a command"
puts "type add to add a movie to the list"
choice = gets.chomp.downcase
case choice
when "add"
puts "what movie would you like to add"
title = gets.chomp
if movies[title.to_sym].nil?
puts "what would you like the rating of #{rating} (1-4)to be?"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} was added with a rating of #{rating}"
else
puts "that movie already exists"
end
when "update"
puts "what movie would you like to update? (case sensitive)"
title = gets.chomp
if movies[title.to_sym].nil?
puts "#{error}"
else
puts "what is the movie rating would you like to update?"
movies[title.to symb] = rating.to_i
puts "#{title}'s rating has been updated to #{rating}"
end
when "display"
movies.each do |x, y|
puts "#{x} Rating:#{y}"
end
when "destroy"
puts "what movie would you like to erase?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "#{error}"
else
movies.delete(title.to_sym)
puts "the movie no longer exists"
end
else
puts "command not recognized"
end
I'm going to assume that by "Save" you mean stored in the movies hash. The answer to that is that is it is in fact being stored. Only your script exits after you perform an operation, so you never get to see the updated movies.
To see the desired result you're going to want to wrap the majority of this in an infinite loop to prevent the script from naturally exiting.
Consider the following as an example:
store = []
while true
puts "Enter something:"
choice = gets.chomp
store.push choice
puts "Your choices so far are: #{store.inspect}"
end
Related
so the assignment is I'm making a case statement and adding movies paired with their rating in a hash. The "add" case is supposed to add a prompted movie with its paired rating to a hash. It's also supposed to check if the movie already exists in the hash and prompts the user if it does. There's an "update" case which allows the user to update a movie within the hash and also checks if the user inputs a movie that isn't in the hash. Now the update case works fine, it lets the user know if the movies doesn't exist however, I don't think the else part of the add case is working properly? I can add "Wall-E" with a rating of 10 and it doesn't prompt me saying it's already there.
movies = {
"Wall-E" => 10
}
puts "Please choose to add, update, display or delete movies"
choice = gets.chomp
case choice
when "add"
puts "Please name a movie you wish to add"
title = gets.chomp
puts "Please give a rating for this movie"
rating = gets.chomp
if movies[title.to_sym].nil? == true
movies[title.to_sym] = rating.to_i
puts "#{title} was added with a rating of #{rating}"
else
puts "The movie you entered has already been added"
end
when "update"
puts "Please name a movie you wish to update"
title = gets.chomp
if movies[title].nil? == true
puts "Hey bud, that one doesn't exist but you can add it if you want"
else
puts "Please give a rating for this movie"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
end
I'm very new to Ruby and practicing with user input. I have coded the following which allows the user to input names of students continuously until they hit return twice. After each input the program returns how many students are in the school, and when they are finished inputting for good, it prints out a list of the students and the cohort they are in.
At the moment, the cohort is hardcoded, and I want to modify this so that I can ask for both the name and the cohort, with the program continuing to ask for this info until the user hits return twice. Any help would really be appreciated - thanks :)
puts "Please enter the names of the students"
puts "To finish, just hit return twice"
students = []
name = gets.chomp
while !name.empty? do
students << {name: name, cohort: cohort}
puts "Now we have #{students.count} students"
name = gets.chomp
end
students
end
def print_header
puts "The students of this Academy".center(50)
puts "-----------".center(50)
end
def print(students)
students.each do |student, index|
puts "#{student[:name]} #{student[:cohort]} cohort"
end
end
end
def print_footer(names)
puts "Overall, we have #{names.count} great students".center(50)
end
students = input_students
print_header
print(students)
print_footer(students)
Instead of a while loop, I'd suggest to use loop do and break in case the name is empty (or also cohort):
loop do
puts "Please enter the names of the students"
name = gets.chomp
break if name.empty?
puts "Please enter the cohort"
cohort = gets.chomp
# break if cohort.empty?
students << {name: name, cohort: cohort}
end
I can't find the error, it keeps telling me: Oops, try again. It looks like you didn't add to the movies hash!
Thank you.
movies = { 'himym' => 5,
'oitnb' => 4 }
puts "Which one do you like better?"
choice = gets.chomp
case choice
when "add"
puts "What is your favorite movie?"
title = gets.chomp
puts "What would you rate it?"
rating = gets.chomp
movies = {}
movies[title] = rating
puts "#{title} with rating #{rating} has been added!"
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end
I tested your code - I got stuck in the same part. Instructions at CodeAcademy are not always clear. Your code, according to what CA is asking is right, you just need to write "add" as an answer for "Which one do you like better?" when testing code at CA's virtual machine. So:
choice = "add"
That way you will be running your case/when filter. Got it?
I have to update and add a movie title and its associated rating to an existing hash. This is the code that i have so far:
movies = {
Titanic:4,
Bestman: 3,
Agora: 2
}
puts "What would you like to do?"
choice = gets.chomp
case movies
when "add"
puts "What movie do you want to add?"
title = gets.chomp
puts "What's the rating of the movie?"
rating = gets.chomp
movies[title] = rating
puts "#{title} has been added with a rating of #{rating}."
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end
When I run the code, I get this error "it looks like you didn't add to the movies hash"
I know the error lies somewhere between these lines:
case movies
when "add"
puts "What movie do you want to add?"
title = gets.chomp
puts "What's the rating of the movie?"
rating = gets.chomp
movies[title] = rating
puts "#{title} has been added with a rating of #{rating}."
I have been trying to figure it out but thus far have failed to figure out what I am doing wrong.
Is there an alternative way to add to my movies hash? And what is wrong with my puts code for letting the user know that his/her movie title and rating has been added?
Thank you
EDIT
As pointed out by Some Guy, changing the case statement from
case movies
to
case choice
resolved the issue.
What I need to learn/figure out is why the 2nd works but the 1st does not.
Change
case movies
to
case choice # because this is where your choice of what to do is being stored
If it's still not clear, it may help to think of a case statement as a series of if/elsif (there are differences, but for this scenario it works), so:
case movies
when "add" # do stuff
is akin to:
if movies == "add"
# do stuff
end
Hoping that makes it clearer.
I'm working on a simple text-based dungeon game in Ruby, and I've run into a snag. Basically, I have one room with a locked door. The key is found in another room and I want the door to be unlocked once the key has been found.
Here's what I've got so far:
def chest_room
puts "You are in a small, round room."
puts "On the floor in front of you is a small wooden chest."
puts "It does not appear to be locked."
puts "What do you do?"
chest_open = false
while true
prompt; next_move = gets.chomp
if next_move == "open chest"
chest_open = true
puts "Inside the chest, you see a small, brass key."
puts "What do you do?"
prompt; next_move = gets.chomp
elsif next_move == "take key" and chest_open
key_present = true
puts "You take the key and slip it into a pocket."
puts "What do you do?"
prompt; next_move = gets.chomp
elsif next_move == "go back"
start()
else puts "I don't understand you."
puts "What do you do?"
prompt; next_move = gets.chomp
end
end
end
def start
puts "You find yourself in a dank room lit by torches."
puts "There are three doors leading out of here."
puts "What do you do?"
door_open = false
key_present = false
while true
prompt; next_move = gets.chomp
if next_move == "door 1"
chest_room()
elsif next_move == "door 2"
dais()
elsif next_move == "door 3" and not door_open
puts "This door is securely locked."
puts "You'll need to find some way of opening it before you can enter."
puts "What do you do?"
prompt; next_move = gets.chomp
elsif next_move == "door 3" and key_present
door_open = true
puts "The key you found fits easily into the lock."
puts "With a click, you unlock the door!"
orb_room()
else
puts "I don't understand you."
puts "What do you do?"
prompt; next_move = gets.chomp
end
end
end
Any input or advice? Essentially, I want to end the door_open = false loop once the key is found, but I can't figure out how to set door_open = true in the chest_room method then call it from the start method. Thanks!
What you really need is to back up and think out the design again. You have a Room, and it has a property "open" or "closed". You have a Key, and you need the key to change the state of that open/closed property.
Write out what you want to do, and think about how to model it as a Room and a Key, and you'll be on a better track.
(No, I don't want to give you the answer. Figuring it out is more important.)
You're having scope issues. make the door_open or key_present instance variables by putting an # in front of them. Then any method can access them. also case/when is cleaner than if elsif
while !#key_present # #key_present can be modified in another method
prompt; next_move = gets.chomp
case
when "door 1" == next_move then chest_room() # put constants first when comparing ==
when "door 2" == next_move then dais()
# more conditions
end
end