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 () 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!
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.
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.
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.
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.
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.
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.
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’.
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.
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
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?
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
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 !
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?