Loops, $end, kEND, kELSE

Hi! This is the ‘Deaf Grandma’ exercise from Chris P.'s Learn to
Program Second Edition book in which there is an instruction to make a
program that what is said to grandma is not heard unless it is in
capital letters.
In the extended version of this exercise when you should BYE three times
in a row she finally lets you leave.

Im having trouble understanding the error messages Ruby is giving me
namely:

deaf.rb:13: syntax error, unexpected kELSE, expecting kEND
else input == input.upcase
^
deaf.rb:17: syntax error, unexpected kELSE, expecting $end
else input == ‘BYE’
^

Here is my code:

/Deaf Grandma/

while input != ‘BYE’
puts ‘Say hello to your grandmother’
input = gets.chomp

if input == input.downcase
puts ‘HUH?! SPEAK UP, SONNY!’
end

else input == input.upcase
puts (‘NO, NOT SINCE’ + (rand(21)+1930).to_s + ‘!’)
end

else input == ‘BYE’
break
end

Thank you
Coralie

On 01/29/2014 06:25 PM, Coralie H. wrote:

Hi! This is the ‘Deaf Grandma’ exercise from Chris P.'s Learn to
Program Second Edition book in which there is an instruction to make a
program that what is said to grandma is not heard unless it is in
capital letters.
In the extended version of this exercise when you should BYE three times
in a row she finally lets you leave.

Im having trouble understanding the error messages Ruby is giving me
namely:

This error messages are cryptic almost always indicate you have
misplaced an “else” and an “end” somewhere.

deaf.rb:13: syntax error, unexpected kELSE, expecting kEND
else input == input.upcase
^

This means Ruby was expecting to see an “end” keyword (kEND), but
instead got “else”. In other words, you have an “else” keyword where it
shouldn’t be.

deaf.rb:17: syntax error, unexpected kELSE, expecting $end
else input == ‘BYE’
^

Similarly, Ruby was expecting the end of the file ($end) but instead got
another “else” keyword.

input = gets.chomp
break
end

Thank you
Coralie

In Ruby if/else looks like this:

if some_condition
do_some_stuff
else
do_some_other_stuff
end

Compare this to your code.

-Justin

Also, here’s a hint: the keyword ‘elsif’ exists.

On 2014-Jan-29, at 21:25 , Coralie H. [email protected]
wrote:

deaf.rb:13: syntax error, unexpected kELSE, expecting kEND
else input == input.upcase
^
deaf.rb:17: syntax error, unexpected kELSE, expecting $end
else input == ‘BYE’
^

read kELSE as “keyword ‘else’”
read kEND as “keyword ‘end’”
read $end as “end of file” (aka, nothing left to parse)

So on line 13, for example, the parser found an unexpected keyword
‘else’ when it was expecting (looking for) an ‘end’ (to match the
‘while’)
and on line 17, the parser thought it could be done when it found the
keyword ‘else’ (since the ‘end’ on line 15 was the one it was expecting
since back on line 13)

Better?

-Rob

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs