"Redacted!" "Control Flow Know-How" error

https://www.codecademy.com/courses/ruby-beginner-en-mzrZ6/0/5
I used this code:

puts"I want all of your text!"
text=gets.chomp
puts"Now, what do you want to redact kid?"
redact=gets.chomp
words=text.split(" ")
words.each do |word|
    print word
    if word==redact:
        print"REDACTED "
    else
        print word +" "
    end
end

But, in the console, it says this:

(ruby):7: syntax error, unexpected tLABEL
(ruby):9: syntax error, unexpected keyword_else, expecting keyword_end
(ruby):12: syntax error, unexpected keyword_end, expecting $end

and this popup comes:

There’s inconsistency. You used words and later on word. The computer seems confused now.

Hope this helps! :slight_smile:

@bayoishola20

So I should change one of them into something else?

This exercise is a continuation of previous exercise. So you should have it as words. See below:

words.each do |words|
    
    if words == redact
        print "REDACTED "
    else 
        print words + " "
    end
end
2 Likes

@bayoishola20[quote=“bayoishola20, post:4, topic:44585, full:true”]
This exercise is a continuation of previous exercise. So you should have it as words. See below:

words.each do |words|

if words == redact
    print "REDACTED "
else 
    print words + " "
end

end
[/quote]

I tried, it still has the same output.

You should post your new code and the error.

However, I noticed something. See corrected below:

text = gets.chomp
puts text
redact = gets.chomp
puts redact

words = text.split(" ")

words.each do |words|
    
    if words == redact
        print "REDACTED "
    else 
        print words + " "
    end
end

Hope it helps! :slight_smile:

1 Like

@bayoishola20

That code worked, but why?

Err… For one, [quote=“printcoder_eric, post:1, topic:44585”]
if word==redact:
[/quote]

You had a colon. It could have been confusing the computer.

Yes that colon should not have been there - that looks more like python syntax. Also the first print word statement would not be necessary. Finally you were correct in having words and word. one represents the array of words and the other is the arbitrary variable used to mean a single word (which you could have called anything really.

2 Likes

Yup. It is a python syntax. I didn’t observe that part of the person’s code until later.

Way to go! :slight_smile:

Try this one…

words = [‘zok’,‘yew’,‘xor’,‘wor’,‘vyl’]
puts words
puts ">>>Input selected string: "
text = gets.chomp
puts text

words.each do |words|
if words == text
print "REDACTED "
else
print words + " "
end
end

1 Like

Why do we have to put end twice? My code wouldn’t work without it, but why??? I haven’t had to do it up until this exercise, so I’m confused.

my code below, and there is always the same answer “Oops, try again. Make sure to print each word from the user’s text to the console unless that word is the word to be redacted; if it is, print REDACTED (all caps!).”
it seems fine but i can not move on. Can you help?

puts "Text to search through: "
text = gets.chomp
puts "Word to redact: "
redact = gets.chomp

words = text.split(" ")

words.each do |words|
if words == redact
print "REDACTED "
else
print words + “”
end
end

Please, reorder users input and “puts” like this:

text = gets.chomp
puts text
redact = gets.chomp
puts redact

Also, leave a space between quotation marks, here.

to have:

print words + " "

You don’t necessarily have to change anything.
For me it worked using “word” instead of “words”.

The thing that matters is the “end” keyword, my solution was to put it twice, the first time for the if/else statement and the second time for the loop.