Argh?! :) kEND, $end and kELSE in a simple program

J

Jet Koten

Hi,

I'm completely new to programming and am learning using Ruby.

I'm trying to get this program working using only these basic commands
that are in my program:

puts 'Say something to the virtual hard-of-hearing Grandpa:'
while true
said=gets.chomp
year = (1930+rand(51))
counter=0
if said=='GOODBYE'
counter+=1
if counter==3
break
end
end
elsif said !=said.upcase
puts 'HUH?! SAY IT LOUDER!'
counter-=1
end
else
puts 'NO, NOT SINCE '+year.to_s+'!'
counter-=1
end
end

So basically it's supposed to be that it'll just loop forever unless you
say GOODBYE three times in a row... and not just three times with
something else in between.

Otherwise if you say anything lowcase it'll say HUH?! [...] and if you
say anything upcase it should say NOT SINCE and a random number between
1930 and 1950. I had all the basic functionality working like this:

puts 'Say something to the virtual hard-of-hearing Grandpa:'
while true
said=gets.chomp
year = (1930+rand(21))
if said !=said.upcase
puts 'HUH?! SAY IT LOUDER!'
else puts 'NO, NOT SINCE '+year.to_s+'!'
end
end


I'm really stuck about getting 'him' to break after the 3 GOODBYEs in a
row, I've written the code the best I can and now all I get are various
combinations of kEND, $end and kELSE. I'm guessing it's some kind of
scope issue but I don't know enough to know what I don't know... you
know? :)

Can anyone see what will get 'him' working using just the same basic
commands that are in this program already?

Thanks in advance.
 
X

Xavier Noëlle

2010/1/22 Jet Koten said:

Hi !
puts 'Say something to the virtual hard-of-hearing Grandpa:'
while true
=A0said=3Dgets.chomp
=A0year =3D (1930+rand(51))
=A0counter=3D0
=A0if said=3D=3D'GOODBYE'
=A0counter+=3D1
=A0 =A0if counter=3D=3D3
=A0 =A0 =A0break
=A0 =A0end
=A0end
=A0elsif said !=3Dsaid.upcase

You closed your 'if' scope too early, then the 'elsif' became orphan
(=3D> remove the 'end').
So basically it's supposed to be that it'll just loop forever unless you
say GOODBYE three times in a row... and not just three times with
something else in between.

The problem is that you reset the counter at the beginning of each
loop iteration.
"counter =3D 0" gets called after your program has determined what you
said (=3D> put it before the loop and that should do the trick ;-))

--=20
Xavier NOELLE
 
R

Rob Biedenharn

Hi,

I'm completely new to programming and am learning using Ruby.

I'm trying to get this program working using only these basic commands
that are in my program:

puts 'Say something to the virtual hard-of-hearing Grandpa:'
while true
said=gets.chomp
year = (1930+rand(51))
counter=0
if said=='GOODBYE'
counter+=1
if counter==3
break
end
end
elsif said !=said.upcase
puts 'HUH?! SAY IT LOUDER!'
counter-=1
end
else
puts 'NO, NOT SINCE '+year.to_s+'!'
counter-=1
end
end

So basically it's supposed to be that it'll just loop forever unless
you
say GOODBYE three times in a row... and not just three times with
something else in between.

Otherwise if you say anything lowcase it'll say HUH?! [...] and if you
say anything upcase it should say NOT SINCE and a random number
between
1930 and 1950. I had all the basic functionality working like this:

puts 'Say something to the virtual hard-of-hearing Grandpa:'
while true
said=gets.chomp
year = (1930+rand(21))
if said !=said.upcase
puts 'HUH?! SAY IT LOUDER!'
else puts 'NO, NOT SINCE '+year.to_s+'!'
end
end


I'm really stuck about getting 'him' to break after the 3 GOODBYEs
in a
row, I've written the code the best I can and now all I get are
various
combinations of kEND, $end and kELSE. I'm guessing it's some kind of
scope issue but I don't know enough to know what I don't know... you
know? :)

Can anyone see what will get 'him' working using just the same basic
commands that are in this program already?

Thanks in advance.

You have extra 'end's where you don't need them (see the ones I
commented out):

puts 'Say something to the virtual hard-of-hearing Grandpa:'
while true
said=gets.chomp
year = (1930+rand(51))
counter=0
if said=='GOODBYE'
counter+=1
if counter==3
break
end
#end
elsif said !=said.upcase
puts 'HUH?! SAY IT LOUDER!'
counter-=1
#end
else
puts 'NO, NOT SINCE '+year.to_s+'!'
counter-=1
end
end

In general, kEND or kELSE means that is a keyword either missing of
found where it shouldn't be. And $end means the end of the file.

So if you get an error like:
"syntax error, unexpected $end, expecting kEND"

It means:
"you seem to have broken one of Ruby's syntax rules,
I ran out of stuff in this file to look at
while I was looking for an 'end' keyword"

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
R

Rob Biedenharn

Hi,

I'm completely new to programming and am learning using Ruby.

I'm trying to get this program working using only these basic commands
that are in my program:

puts 'Say something to the virtual hard-of-hearing Grandpa:'
while true
said=gets.chomp
year = (1930+rand(51))
counter=0
if said=='GOODBYE'
counter+=1
if counter==3
break
end
end
elsif said !=said.upcase
puts 'HUH?! SAY IT LOUDER!'
counter-=1
end
else
puts 'NO, NOT SINCE '+year.to_s+'!'
counter-=1
end
end
I'm really stuck about getting 'him' to break after the 3 GOODBYEs
in a
row, I've written the code the best I can and now all I get are
various
combinations of kEND, $end and kELSE. I'm guessing it's some kind of
scope issue but I don't know enough to know what I don't know... you
know? :)


And to explain the error from this code:

-:12: syntax error, unexpected kELSIF, expecting kEND
elsif said !=said.upcase
^
-:16: syntax error, unexpected kELSE, expecting $end

In more human terms (don't worry, soon you'll only see the redhead):

"I found a problem on line 12 when I saw 'elsif' because I was
expecting something like 'end' because I was inside a 'while' block at
the time. Here's the point on line 12 that I was sure I had a problem.

Oh, and then on line 16 I found an 'elsif', but I just thought the
file would be finished by now."

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
J

Jet Koten

Xavier said:
The problem is that you reset the counter at the beginning of each
loop iteration.
"counter = 0" gets called after your program has determined what you
said (=> put it before the loop and that should do the trick ;-))

Thanks very much for your help! Well, although I tried moving the
counter out of the loop (and what you said now makes perfect sense to me
that it was getting reset each time...) it now works without kENDing or
$ending ot kELSING, but when I write GOODBYE it just keeps letting me
write GOODBYE more than three times (forever actually, I would guess).

So, now I'm wondering why my counter logic isn't working?
 
J

Jet Koten

Rob Biedenharn wrote:

Thanks a lot Rob, it makes it a lot easier for me to know what Ruby is
thinking in human terms. I'm enjoying it greatly and am trying to build
up my Ruby ability to get ready to try Rails in the future. This
forum/list is truly a great resource with people like you and Xavier
willing to help the new people so readily.
 
R

Roger Pack

Judging from your error message, you might get a more helpful error
message by running ruby 1.9 (if you're not) and/or running it ruby -w,
which gives you indentation warnings.
-r
 
J

Jet Koten

And for everyone's reference, my code now looks like this:

puts 'Say something to the virtual hard-of-hearing Grandpa:'
counter=0
while true
said=gets.chomp
year = (1930+rand(51))
if said=='GOODBYE'
counter+=1
if counter==3
break
end
elsif said !=said.upcase
puts 'HUH?! SAY IT LOUDER!'
counter-=1
else
puts 'NO, NOT SINCE '+year.to_s+'!'
counter-=1
end
end

and the problem with it is that it doesn't break after getting GOODBYE
three times in a row...
 
R

Roger Pack

puts 'Say something to the virtual hard-of-hearing Grandpa:'
counter=0
while true
said=gets.chomp
year = (1930+rand(51))
if said=='GOODBYE'
counter+=1
if counter==3
break
end
elsif said !=said.upcase
puts 'HUH?! SAY IT LOUDER!'
counter-=1
else
puts 'NO, NOT SINCE '+year.to_s+'!'
counter-=1
end
end

you might benefit from the rbeautify gem, too
$ gem install rbeautify
$ rbeautify script_name.rb
 
J

Jet Koten

Roger said:
you might benefit from the rbeautify gem, too
$ gem install rbeautify
$ rbeautify script_name.rb

I am grateful, I installed it and ran it and it did a good job. Anyone
have any idea why the 'Grandpa' won't break though?
 
R

Rob Biedenharn

And for everyone's reference, my code now looks like this:

puts 'Say something to the virtual hard-of-hearing Grandpa:'
counter=0
while true
said=gets.chomp
year = (1930+rand(51))
if said=='GOODBYE'
counter+=1
if counter==3
break
end
elsif said !=said.upcase
puts 'HUH?! SAY IT LOUDER!'
counter-=1
else
puts 'NO, NOT SINCE '+year.to_s+'!'
counter-=1
end
end

and the problem with it is that it doesn't break after getting GOODBYE
three times in a row...


But it probably eventually hits the break if you keep saying "GOODBYE"

Either run the debugger or add a line right after the while true
puts "the counter is #{counter}"

Then it should become clear.

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
J

Jet Koten

Oops, I missed this message when I was reading the other replies.

I'm running 1.8.7 on Mac OS X 10.4 (Tiger). When I installed Macports
and then installed a newer Ruby than is on OS X 10.4 it set me up with
1.8.7. I renamed the old 10.4 ruby so it doesn't run anymore when I type
ruby.

I'd like to run Rails, and especially Spree e-commerce.

Should both of them and all the various gems and/or plugins they require
work OK with Ruby 1.9, and if so, how do I even install it with Macports
(the only way I'm comfortable installing things) if I already asked it
for Ruby and it gave me 1.8.7... ?

Thanks again to everyone for all the help getting up and running. :)
 
J

Jet Koten

Rob said:
But it probably eventually hits the break if you keep saying "GOODBYE"

Either run the debugger or add a line right after the while true
puts "the counter is #{counter}"

Then it should become clear.

OK! You are absolutely correct. What was happening is that I was testing
out to make sure that the regular downcase and upcase conditions were
still working before I tried the GOODBYE again.

So, now I've solved the reason as to why it wasn't breaking, but I'm
back to the drawing board about how to get it to quit after three
GOODBYEs no matter what else has already been said beforehand.
 
N

Nick Brown

Rob, if you wrote an "error to English" translator and submitted it as a
patch to Ruby, you would be a hero among men.
 
J

Jet Koten

Well, I've got it working now. In case anyone is interested, the final
working version is:

puts 'Say something to the virtual hard-of-hearing Grandpa:'
counter=0
while true
puts "the counter is #{counter}"
said=gets.chomp
year = (1930+rand(51))
if said=='GOODBYE'
counter+=1
if counter==3
break
end
elsif said !=said.upcase
puts 'HUH?! SAY IT LOUDER!'
counter=0
else
puts 'NO, NOT SINCE '+year.to_s+'!'
counter=0
end
end


and my problem was I was decreasing my counter in the elsif and else
when I really should have been zeroing it.

I still don't know about running ruby 1.9 though. I'd like to run it but
the answers to my questions below are still unknown to me. Should I
start a new thread just about these questions, or will someone let me
know here?

Above all, thanks for the help. Just the right amount of instruction
without spoiling the fun of figuring it out myself what I had done
incorrectly.

Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top