Congratulationth! Help please


#1


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

Hey guys,

I'm trying to include the "while" but whenever I do the system simply ignores the first part:

if user_input.include? "s"
user_input.gsub!(/s/, "th")
else
"There was no S here"
end

So, if I remove the "while" it works well and it replaces the /s/ to "th" but if I add the "while" code, the while works but it wont replace the /s/ to "th" anylonger, am I doing wrong here? My code below: thanks for the help


print "Anything please"
user_input = gets.chomp
user_input.downcase!


if user_input.include? "s"
    user_input.gsub!(/s/, "th")
else
    "There was no S here"
end

while user_input.empty?
puts "You have to reentry"
user_input = gets.chomp
end

puts "Your string now is #{user_input}"


#2

theres no escape from your loop. you would be better off adding the if statement into the while loop also. i am not really sure why the while loop takes over for the if though. when you look it up they have the same level of precedence. if you had a break and the if after it would work.


#3

Actually it didnt work! the ELSE command stops working! I get the loop, but if I type anything other than S it wont trigger the ELSE! :frowning:

print "Anything please"
user_input = gets.chomp
user_input.downcase!

while user_input.empty?
user_input = gets.chomp
puts "You have to reentry"

end

if user_input.include? "s"
user_input.gsub!(/s/, "th")
else
"There was no S here"
end

puts "Your string now is #{user_input}"


#4

How would it look or how would it be different?


#5

Oh... got it now! it was because of the PUTS! If i remove it, it works! haha :slight_smile: thanks mate


#6

Sorry didn't see the other post in your while statement you can have the puts just add

break

After the puts and before the end keyword


#7

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