FAQ: Thith Meanth War! - Congratulationth!

This community-built FAQ covers the “Congratulationth!” exercise from the lesson “Thith Meanth War!”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Congratulationth!

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 (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 (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

print "What's uuuup?"
user_input = gets.chomp
if (user_input.downcase.include? "s") || (user_input.downcase.include? "ce") || (user_input.downcase.include? "ci") 
  user_input.gsub!( /[sS]/ , "s" => "th" , "S" => "TH")
  user_input.gsub!( /ce/ , "the" )    
  user_input.gsub!( /Ce/ , "The" )  
  user_input.gsub!( /CE/ , "THE" )  
  user_input.gsub!( /ci/ , "thi" ) 
  user_input.gsub!( /Ci/ , "Thi" )  
  user_input.gsub!( /CI/ , "THI" )  
  print "Your string is #{user_input}"
elsif user_input.empty?
  puts "Try again"
else
  puts "No letters to change, try again"
end

:white_check_mark: Add an additional if statement to re-prompt the user for input if they don’t enter anything


:white_check_mark:Think about how you might account for words in which the letter “c” sounds like an “s”
(Solved-ish. But I wanted to see if there could be a way to solve it using one line of .gsub

user_input.gsub!( /[sc]/ , "th")

Problem:
If you the user writes “I’m fine, come on!” , the result is “No letters to change, try again”, which is fine.
However, if the user writes “I’m super fine, Cecilia! come on!” , the result is “I’m thuper fine, thethilia! thome on!”

So, I tried writing:

user_input.gsub!( /[scei]/ , "s" => "th" , "ce" => "the" , "ci" => "thi")

But for some reason it doesn’t allow substituting a sequence of more than one character? Like it allows:
"s" => "loremipsum" but the other way around it returns nil
The result is “I’m thuper fine, cilia! comeon!”


:white_check_mark:Think about how you might preserve the user’s original capitalization
I tried to do something with .casecmp but I couldn’t come up with anything :cold_sweat:
I’m not 100% happy about this, cause I feel like there could be a better way of writing this.
Maybe having a way to say substitute “c” to “th” with the exception of when it comes after “-a”, “-o” and “-u”?

If anyone knows a way around this, please let me know!

2 Likes

You can preserve the capitalization by simply not calling .downcase if you are already looking for both s and S.

Here is a way to keep asking for input while user_input is empty with a while statement.

user_input=""
while user_input.empty?
print "Tell me something?"
user_input=gets.chomp.downcase
end

if user_input.include? "s"
    user_input.gsub!(/s/, "th")
else "No S's found!"
end
puts "You said, #{user_input}!"
puts "End of program."
puts "Type what you want Donald Duckified:"
user_input = gets.chomp

if user_input.empty?
  puts "Please type something!"
  user_input = gets.chomp
end

if user_input.include?"s"
  user_input.gsub!(/s/, "th")
  
  user_input.include?"S"
  user_input.gsub!(/S/, "Th")
  
  user_input.include?"cc"
  user_input.gsub!(/cc/, "th")
  
  print user_input
else puts "There's nothing to Duckify!"
end

Finally figured it out! You don’t need to put each S, s, cc, etc. into separate if statements. They all go together.

1 Like

I spent probably way too long on this. Here’s my code. I imagine there would be an easier way to do this instead of having three separate bits of code for capitalised, lower case and all caps substitutions…

def daffyduckifier
print "Write what you want to be Daffy Duckified here… "
user_input = gets.chomp

#removes “sh”. This prevents it from being affected by the program. We put it back in after substitutions have been made.
user_input.gsub!(/sh/, “XXXTHINGYXXX”)
user_input.gsub!(/Sh/, “YYYTHINGYYYY”)
user_input.gsub!(/SH/, “ZZZTHINGYZZZ”)

#begin substitutions…

if user_input.downcase.include? “s”

#lowercase substitutions
user_input.gsub!(/muscle/, “muthle”)
user_input.gsub!(/psych/, “thych”)
user_input.gsub!(/psionic/, “thionic”)
user_input.gsub!(/soccer/, “XXXthocXXXer”) #prevents “soccer” being turned into “thocther”
user_input.gsub!(/sce/, “the”)
user_input.gsub!(/sci/, “thi”)
user_input.gsub!(/scy/, “thy”)
user_input.gsub!(/ce/, “the”)
user_input.gsub!(/XXXthocXXXer/, “thoccer”)
user_input.gsub!(/ci/, “thi”)
user_input.gsub!(/cy/, “thy”)
user_input.gsub!(/s/, “th”)

#capitalised substitutions
user_input.gsub!(/Muscle/, “Muthle”)
user_input.gsub!(/Psych/, “Thych”)
user_input.gsub!(/Psionic/, “Thionic”)
user_input.gsub!(/Soccer/, “XXXThocXXXer”) #prevents “soccer” being turned into “thocther”
user_input.gsub!(/Sce/, “The”)
user_input.gsub!(/Sci/, “Thi”)
user_input.gsub!(/Scy/, “Thy”)
user_input.gsub!(/Ce/, “The”)
user_input.gsub!(/XXXThocXXXer/, “Thoccer”)
user_input.gsub!(/Ci/, “Thi”)
user_input.gsub!(/Cy/, “Thy”)
user_input.gsub!(/S/, “Th”)

#uppercase substitutions
user_input.gsub!(/MUSCLE/, “MUTHLE”)
user_input.gsub!(/PSYCH/, “THYCH”)
user_input.gsub!(/PSIONIC/, “THIONIC”)
user_input.gsub!(/SOCCER/, “XXXTHOCXXXER”) #prevents “soccer” being turned into “thocther”
user_input.gsub!(/SCE/, “THE”)
user_input.gsub!(/SCI/, “THI”)
user_input.gsub!(/SCY/, “THY”)
user_input.gsub!(/CE/, “THE”)
user_input.gsub!(/XXXTHOCXXXER/, “THOCCER”)
user_input.gsub!(/CI/, “THI”)
user_input.gsub!(/CY/, “THY”)

#puts “sh” back
user_input.gsub!(/XXXTHINGYXXX/, “sh”)
user_input.gsub!(/YYYTHINGYYYY/, “Sh”)
user_input.gsub!(/ZZZTHINGYZZZ/, “SH”)

puts “Your result…”
puts user_input
try_again

elsif user_input == “”
puts “You have to write something!”
daffyduckifier

else

#puts “sh” back
user_input.gsub!(/XXXTHINGYXXX/, “sh”)
user_input.gsub!(/YYYTHINGYYYY/, “Sh”)
user_input.gsub!(/ZZZTHINGYZZZ/, “SH”)

puts "You said… "
puts user_input
print "…but nothing needed to be changed. "
try_again

end

def try_again
puts “Try again y/n?”
yn_input = gets.chomp
yn_input.downcase!

if yn_input == “y”
daffyduckifier

elsif yn_input ==“n”
puts “OK!”

else puts “I didn’t understand that…”
try_again
end
end
end

puts “Welcome to the Daffy Duckifier!”
daffyduckifier

print "Pleathe enter a thtring: "
user_input = gets.chomp

until user_input.to_s.length>0
print “Pleathe enter a thtring:”
user_input = gets.chomp
end
if (user_input.include? “s”) || (user_input.include? “S”)
user_input.gsub!(/s/, “th”) && user_input.gsub!(/S/, “Th”)
else
puts “There are no 's’s in your string.”
end
user_input.capitalize!
puts “Your new thtring is #{user_input}.”

print "Pleathe enter a thtring: " 
user_input = gets.chomp
user_input.downcase!

if user_input.empty?
  puts "There are no words here. Please enter some words: "
  user_input2 = gets.chomp
  user_input2.downcase!
  if user_input2.include? "s" || "ce"
    user_input2.gsub!(/s/, "th") && user_input2.gsub!(/ce/, "th")
  puts "Your daffified string is: #{user_input2.capitalize!}"
  end
elsif user_input.include? "s" || "ce"
  user_input.gsub!(/s/, "th") && user_input.gsub!(/ce/, "th")
  puts "Your daffified string is: #{user_input.capitalize!}"
else
  puts "No esses found! Please enter a word with esses: "
  user_input3 = gets.chomp
  user_input3.downcase!
  if user_input3.include? "s" || "ce"
    user_input3.gsub!(/s/, "th") && user_input3.gsub!(/ce/, "th")
  puts "Your daffified string is: #{user_input3.capitalize!}"
  end
end
1 Like

Is there a reason a while statement would be better than using an if statement in this scenario?

Thanks for this - I was struggling. Thought I was missing something obvious.

while and if are different. So now the next thinking step would be: what is the difference in behavior between if and while? And based on these behaviors, which is the best to use here?

your thought process going in the right direction, but its time to take another step forward :slight_smile:

Thanks for the reply, so I can see a while loop continues evaluating and executing while the condition is true.
If evaluates to see if the condition is true and then executes.
Both work in this exercise, but I can’t see which would be ‘better’ (perhaps because my understanding of the two is wrong?).

pick the one that matches what you meant, is it a loop or not?

also, no statements in ruby (what is a statement?)

So, what if the user enters something invalid? What is then the difference between if and while?

Hi, I was just using the language the course on here teaches. See attached screenshot.

Is statement an incorrect term in this context?

It’s all expressions.
Statements have no result value, you cannot do this in python:
x = for i in range(3): print(3)
because a for-loop is a statement, there is no result that x can be assigned to.
but you can do this:
x = print(3)
because calling a function is an expression

but in ruby, everything is an expression, a loop has a result.

irb> x = unless true then 1 end
irb> x
=> nil   <- nil is still a value, statements don't even have this
irb> x = unless false then 1 end
irb> x
=> 1

I suppose you could invent your own meaning of statement for ruby to mean the same thing as expression, but how then would you choose whether to use the word expression or statement?
You’d choose by what some other language uses for the corresponding language feature, so you aren’t describing what’s in ruby, you’re describing how you would do it in a different language and use a word that specifically means something other than the thing you’re using in ruby

Imagine soda being called water because hey that’s the only thing we drink and put in our food. Here, have some water. (though this would be more accurate because there’s water in soda)

You COULD use statement to describe how you are using an expression - using it for its side effect and discarding its result, and if people are to be given the benefit of doubt it would be through this, but when talking about the language construct itself… I don’t like it at all.

I’m super confused.

The Ruby course on this website uses the word ‘statement’ to describe if, unless etc.

Are you saying that this is completely wrong?

If you for example were to describe the syntax of a list literal, you would say it begins with [ and ends with], and within those, there are expressions separated by commas.

Can you put a python loop in a list?
You can put loops in a ruby list literal.

When talking about how different components fit togther, no, there are no statements.

But you could write code and say, I’m making a couple of statements, you’d be talking about the code, not the language component, you wouldn’t be talking about how the language implementation treats what you are writing.

Idk maybe there are well-informed rubyists that would argue differently, but when it comes to the general english ruby community writing stuff on the internet … I have a deep distrust for that.

Cool. I understand all your points, thanks for taking the time to explain it to me.

Why does this course use the term statement?

Well I mean they’re also calling else a statement, it’s not even an expression, it’s part of an if-expression or unless-expression or what have you, you can’t do:

else
   do something
end

So I guess they’re saying how to use things rather than what they are