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


#1

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:


#2

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

Hope this helps! :slight_smile:


#3

@bayoishola20

So I should change one of them into something else?


#4

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

#5

@bayoishola20

I tried, it still has the same output.


#6

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:


#7

@bayoishola20

That code worked, but why?


#8

Err... For one,

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


#9

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.


#10

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

Way to go! :slight_smile:


#11

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


#12

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.


#13

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


#14

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 + " "

#15

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.


#17