FAQs on the exercise When and Then: The Case Statement
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Ok so why did my code work but not be accepted as correct? This is what I put?
puts “What Language?”
greeting = gets.chomp
Add your case statement below!
case greeting
when “English”
puts “Hello!”
when “French”
puts “Bonjour!”
when “German”
puts “Guten Tag!”
when “Finnish”
puts “Haloo!”
else
puts “I don’t know that language!”
end
As mentioned, the program worked but wouldn’t let me move forward. The exercise did say I could use either the format above or what the solution put below
case greeting
when “English” then puts “Hello!”
when “French” then puts “Bonjour!”
when “German” then puts “Guten Tag!”
when “Finnish” then puts “Haloo!”
else puts “I don’t know that language!”
end
The Ruby track is exhibiting signs of an unstable environment in which user inputs become an issue. The workaround is to define the input variables with a literal value and let the code run its course.
For this exercise, we could benefit from an array of possible inputs and go through the list.
This course seems to be ordered in a strange way. Case and when were introduced earlier in the course, but this lesson seems to introduce them as if they are new concepts. I suggest either removing any repeats or making this entire zen lesson a sort of review (unless there are new concepts introduced in it, which seems unlikely).
I’d like to note that the then keyword was not introduced earlier in the other lesson.
I agree, I think it’s the “then” keyword as in the example Codecademy shows all the “then” keywords are in light blue, but when typed into the script.rb, the “then” keywords are shown red, not light blue.
My code below (which I think is correct but to move onto the next topic I probably can’t used the below code, aka, the “then” keyword):
Not a big deal. It could be from the older learning environment/editor, which has changed periodically on the site. The code makes sense, and the syntax highlighting is consistent so don’t read any more into it.
Lesson worked ok for me.
My solution strives to be pretty & readable:
ruby code
puts "Hello there!"
greeting = gets.chomp
puts case greeting
when "English" then "Hello!"
when "French" then "Bonjour!"
when "German" then "Guten Tag!"
when "Finnish" then "Haloo!"
else "I don't know that language!"
end
For those struggling in this exercise, this worked for me:
greeting = []
puts "Hello there!"
greeting = gets.chomp
greeting.downcase
# Add your case statement below!
case greeting
when "english" then puts "Hello!"
when "french" then puts "Bonjour!"
when "german" then puts "Guten Tag!"
when "finnish" then puts "Haloo!"
else
puts "I don't know that language!"
end
this is my code, identical to what i know its suppose to be and still it does’nt let me progress
puts “Hello there!”
greeting = gets.chomp
Add your case statement below!
case greeting
when “English” then puts “Hello!”
when “French” then puts “Bonjour!”
when “German” then puts “Guten Tag!”
when “Finnish” then puts “Haloo!”
else puts “I don’t know that language!”
end
thank you so much for replying to me. I’m not quite sure what you mean to setting greeting manually? Nothing is printing in my console and the solution is the exact same as what I’ve put. Really apprecite you helping, I’ve been stuck on this for hours. This is what prints
When you click Run, be sure to click where the prompt is in the console and quickly enter your text and press Enter. There is only a short time window before that input prompt times out.
By manual setting, we assign the literal value rather that gets.chomp.
wow it worked! Thank you so much! So if this situation happens again, do I always have to apply the literal value rather than gets.chomp? If so how comes?
We cannot judge the situation as always, just as a fallback if the input (gets.chomp) expires too quickly, or won’t take input at all. It at least lets us see the code run through to completion.