FAQ: Thith Meanth War! - Setting Up the 'If' Branch, Part 1

This community-built FAQ covers the “Setting Up the ‘If’ Branch, Part 1” 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 Setting Up the ‘If’ Branch, Part 1

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!

hi, I’m struggling with this one as I don’t feel like the guidance is clear.

[quote] We want to check user_input for the substring "s" .

Write an if statement in the editor. It should check to see if user_input includes "s" .

For now, print a string of your choice to the console if it finds "s" .[/quote]
The only hint is to “go back to the first exercise”. However, the first exercise talks about replacing the “s” with a “th” - not just printing something if it finds one. This means I’m not clear on how to phrase this to get a useful answer.
I may be overthinking this.

The lesson text gives a perfect example to follow (just scroll up a few lines)…

if string_to_check.include? "substring"

Take out string_to_check and replace it with user_input, and shorten "substring" to just "s".

print "Enter something with an `s` in it: "
user_input = gets.chomp
# your if statement
1 Like

Hi, thanks for jumping in!
It’s not that bit that’s the issue - it’s how to then get it to print something if it finds an ‘s’.
I’ve got:
print "Give me someting to work with: "
user_input = gets.chomp
user_input = user_input.downcase!

if user_input.include? “s”
print “There is an s.”
end

I think I’m missing something fundamental about how to get this to print something if it finds an ‘s’, which is probably really obvious. :smiley:

Running it gives me:
Give me someting to work with: fjksdal
undefined method `include?’ for nil:NilClass

1 Like

The bang method does not return anything since the work is done in-place on the object. If there are no uppercase letters in the user input, the outcome will be nil. Remove the bang method.

user_input = user_input.downcase

Now it won’t matter if there are any uppercase letters or not.

4 Likes

Ahah! Thank you!
This is my first experience with asking a question on the forum, so thanks for the quick response, and for giving a clear explanation.

1 Like

I’m having an issue with this problem. My code is:

print "give me a string please: "
user_input = gets.chomp.downcase!

if user_input.include? “s”
print “it contains s”
end

I’m getting the following response:

give me a string please: h
undefined method `include?’ for nil:NilClas

Any ideas?!

What happens when you input all lowercase? Now try entering a string with at least one uppercase letter. Now what happens?

The bang method (!) copies only the result and then only if a change as occurred. If the method makes no changes on the input string, it is not copied to the result, and so nil is the return.

2 Likes

Hello, @jeichlersummers! Welcome to the forum. It took some experimenting to figure this one out. After noticing that your code works fine as long as you include an uppercase letter in the original input, I found this:

Downcases the contents of str, returning nil if no changes were made.

from here: downcase! (String) - APIdock

It appears that when you chain the downcase! along with gets.chomp that the value ultimately assigned to user_input is nil.
You can still do what you’re attempting, but on two separate lines:

user_input = gets.chomp
user_input.downcase!

Doing it this way, user_input already has a value, so when the downcase! method is called on it, either the new ‘downcased’ value is assigned to user_input, or it retains its original value when the downcase method returns ‘nil’.

Hopefully that made sense. Happy coding!

2 Likes

I made a little change to the provided code:

print "Pleathe enter a thtring: " 
user_input = gets.chomp.downcase!     #changed here
 
if user_input.include? "s"
   print "found s"
end

However, the console showed that undefined methodinclude?’ for nil:NilClass`. Not sure if there’s any syntax restriction in Ruby that forbids this way of chaining methods.

Something to note about the bang method, !

  • It is in situ;
  • if no change takes place, the value becomes nil.

The include method is an attribute of String class. nil has no attributes.

s = 'forty-two'
puts s.downcase!    # nil  (blank line)

s = 'Forty-Two'
puts s.downcase!    # forty-two
3 Likes

Could anyone explain why the following code doesn’t work? I understand that the key to the problem is in the .downcase! part, but don’t see why it’s a problem

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

if user_input.include? "s"
  print "This string has an s."
end

Try removing the bang! operator/method.

Hi! for some reason i cannot advance to the next part and it says this: “Your code doesn’t look quite right. Check the first exercise if you need help!”

yet i did exactly that and followed the instructions, but it keeps saying that. this is my code so far:

print "Pleathe enter a thtring: "

user_input = gets.chomp

user_input.downcase!

if user_input.include? "s"

print "Your input has 's'"

end

it worked with my online compiler ide and others yet the codecademy seems to not recognize it. is this a bug? or am i really doing something wrong?

1 Like

I had this same problem, and this fixed it. Thanks!

Bonjour, j’ai un soucis pour passer à l’exercice suivant après avoir fait l’exerice correctement je retrouve de nouveau ce soucis ! J’ai déjà vider le cache, fait un autre compte , changer de navigateur


que puis-je faire svp ?

Essaie de faire attention à deux-trois petites choses, notamment :

  • L’espace entre .include? et “s”
  • Le point d’exclamation après .gsub (et pas d’espace entre ça et la parenthèse)
  • Ce n’est pas pits, mais puts

Dernière chose, pour faciliter la lecture, essaie d’aligner tes lignes de façon claire, exemple :

if ta condition
  on fait quelque chose
else
  on fait autre chose
end

Ce qu’il faut retenir de tout ça, c’est que Codecademy va évaluer ton code pour vérifier qu’il correspond à ce qu’ils attendent comme réponse. Ce genre de petit détail peut faire pencher la balance en ta défaveur. Il n’est pas étonnant de voir des étudiants ne pas comprendre pourquoi leur code n’est pas accepté, alors qu’ils ont tout simplement oublié un point dans la phrase qu’ils devaient faire imprimer.

Ca peut paraître pénible et fort pénalisant pour ce que c’est, mais ça force à vraiment faire attention au code qu’on produit, ce qui s’avérera très important par la suite !

1 Like

Merci, Merci Beaucoup pour vos conseils et votre réponse !!

1 Like

Re bonjour, après avoir retenter plusieurs fois, vérifiant les espaces, faciliter la lecture, je ne peux toujours pas accéder à l’étape suivante… Je suis désolée de vous déranger mais j’aimerais savoir mon erreur et pouvoir m’améliorer… pouvez m’aider de nouveau svp?