A Night at the Movies

ruby

#1

My code below runs ok, there is no error message. However, when I run it for testing it does not change the movies hash. For example, first I run “add” a new movie and then I run “display” it does not display the movie i have just added. Similar to delete, update. Please show me why? thank you in advance.

def movie_list
movies = {
forest_gum: 4,
cast_away: 4,
king_kong: 4.5,
fast_n_furous: 5,
star_war: 3
}
puts "What do you want to do?"
puts "__ Type ‘add’ if you want to add new movie"
puts "__ Type ‘update’ if you want to update rating"
puts "__ Type ‘display’ if you want to display movies vs ratings"
puts "__ Type ‘delete’ if you want to delete movie"
choice = gets.chomp
choice.downcase!
case choice
when "add"
puts "Please input the name of new movie"
title = gets.chomp
if movies[title.to_sym]==nil
puts "Please input the rating of new movie"
rating = gets.chomp
movies[title.to_sym]= rating.to_i
puts "New movie added!"
else
puts "This movie is existed in list"
end
when "update"
puts "Please input the name of movie to be updated"
title = gets.chomp
if movies[title.to_sym]==nil
puts "The movie is not existed in list"
else
puts "Please input the new rating of the movie"
rating = gets.chomp
movies[title.to_sym]= rating.to_i
puts "The rating of movie is updated"
end
when "display"
movies.each {
|movie, rating| puts “#{movie}: #{rating}”
}
when "delete"
puts "Please input the name of movie to be delete"
title = gets.chomp
if movies[title.to_sym]==nil
puts "The movie is not existed in list"
else
movies.delete(title.to_sym)
puts "The movie is deleted"
end
else
puts "Error"
end
end
movie_list
continue = true
while continue do
answer= "Y"
while answer == "Y"
puts "Do you want to continue? Y/N"
answer = gets.chomp
answer.upcase!
if answer == "N"
continue = false
break
elsif answer == "Y"
movie_list
#break
else
puts "Please input Y or N"
end
end
end


#2

You are referring to the movies hash as movies but have it named, movie_list (contrary to instructions). The ratings should be between 0 and 4, as well, but that is not the issue.


#3

Hi there,

I change a little bit to the exercise so that the program can run some rounds for me to check the changes on the initial movies hash but I found that it does not work right and I do not know why?


#4

Please post your updated code. Thanks.

There may be concerns over the initial hash, but we can’t say for sure. Need to see where you are at and do some backtracking.


#5

My code below:

def movie_list
movies = {
forest_gum: 4,
cast_away: 4,
king_kong: 4.5,
fast_n_furous: 5,
star_war: 3
}
puts "What do you want to do?"
puts "__ Type ‘add’ if you want to add new movie"
puts "__ Type ‘update’ if you want to update rating"
puts "__ Type ‘display’ if you want to display movies vs ratings"
puts "__ Type ‘delete’ if you want to delete movie"
choice = gets.chomp
choice.downcase!
case choice
when "add"
puts "Please input the name of new movie"
title = gets.chomp
if movies[title.to_sym]==nil
puts "Please input the rating of new movie"
rating = gets.chomp
movies[title.to_sym]= rating.to_i
puts "New movie added!"
else
puts "This movie is existed in list"
end
when "update"
puts "Please input the name of movie to be updated"
title = gets.chomp
if movies[title.to_sym]==nil
puts "The movie is not existed in list"
else
puts "Please input the new rating of the movie"
rating = gets.chomp
movies[title.to_sym]= rating.to_i
puts "The rating of movie is updated"
end
when "display"
movies.each {
|movie, rating| puts “#{movie}: #{rating}”
}
when "delete"
puts "Please input the name of movie to be delete"
title = gets.chomp
if movies[title.to_sym]==nil
puts "The movie is not existed in list"
else
movies.delete(title.to_sym)
puts "The movie is deleted"
end
else
puts "Error"
end
end
movie_list
continue = true
while continue do
answer= "Y"
while answer == "Y"
puts "Do you want to continue? Y/N"
answer = gets.chomp
answer.upcase!
if answer == "N"
continue = false
break
elsif answer == "Y"
movie_list
#break
else
puts "Please input Y or N"
end
end
end

#6

Can you compare your code to that of the first lesson? There will be many differences.


#7

Yes,
Firstly, because I want to run the program some times so I set all the code similar to the first exercise in a function name movie_list.
then, after calling first time the function, I make a boolean variable call continue and set equal true. I make a while loop with condition of continue so that if user input Y the loop continues to run and call the function again so that the user can make another change on the original movie hash. Finally, when stopping the loop, the program can print all changes having made on the hash.

However, when i test there is no change to the hash.


#8

Have you completed the module with a pass? If so, then we need to move this discussion to the open forum in the Corner Bar, if not, then first this module should be passed using instructions given. The SCT is not expecting a method, only a once through inline sequence.


#9

Hi there,

I am not sure to understand what you said. Please explain more. I can say that my code run without error message but give the wrong output that the hash is not changed.


#10

Did you complete and pass the entire module?


#11

Hi Roy,

Yes, I have completed this module and passed entirely.


#12

Okay, now we can explore the module, off track so it does not interfere with Q&A. The first thing we could look at is the hash and it’s composition.


#13

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