3. The Case Statement Error!


#1



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


It gives me the correct message, but here's what I get in the console
add
add
Error!
nil


movies = {
    Undertale: 10,
    Tem: 10,
    Puntale: 10,
}

choice = puts gets.chomp
case choice
    when "add"
        puts "Added!"
    when "update"
        puts "Updated!"
    when "display"
        puts "Movies!"
    when "delete"
        puts "Deleted!"
    else
    puts "Error!"
end


#2

@cssplayer82860,
Check this line:


#3

Whats wrong with it? I just don't see a problem


#4

@cssplayer82860 There is an extra comma


#5

I just removed it, still says error


#6

@cssplayer82860, Check the first letters of all your movies, they must not be titled


#7

So they shouldn't be Uppercased? I just lowercased them now, still error. Please tell me every single thing i need to do.


#8

You are writing
choice = puts gets.chomp

puts gets.chomp is a command that tells ruby to print what you type in. It's hard to make an analogy here, so I won't.

You are making a variable equal to a command to print something. What you want is the variable to equal what you actually type in

"choice = gets.chomp" should make choice equal to the string you are writing, and not the command to print the string you are writing.

What you are seeing is because you have typed in:
add

Then the system follows the puts command and prints what you typed in:
add

Then the program evaluates the variable you have entered for choice which is not "add" but is "please print 'add'", which most closely fits the else statement in your code and will puts:
Error!

Then, because I'm new to ruby and don't quite understand, it usually appends:
Nil


#9

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