now, i have a pretty good idea which methods i should be using to have
grandma respond with "huh!? speak up, sonny!" whenever something is said
that is not in all capitals & i've got a firm grasp on the fact that for
the "any year at random between 1930 & 1950" i'll need to use the rand
method & define it as rand(21) + 1930. i've just been toying around with
the little i know & haven't been able to get the result i want. i
promise you i've toyed around with branching, looping, etc., but am
still not able to get even remotely close to what the exercise asks of
me.
if anyone's able to get me started in the right direction, i'd
appreciate it. there's also an extension to the exercise & it states,
"what if grandma doesn't want you to leave? when you shout 'bye', she
could pretend not to hear you. change your previous program so that you
have to shout 'bye' three times in a row. make sure to test your
program: if you shout 'bye' three times, but not in a row, you should
still be talking to grandma." but, i'll worry about that after i've got
the previous stuff squared away. thanks in advance.
--
Posted via http://www.ruby-forum.com/.
> if anyone's able to get me started in the right direction, i'd
> appreciate it. there's also an extension to the exercise & it states,
> "what if grandma doesn't want you to leave? when you shout 'bye', she
> could pretend not to hear you. change your previous program so that you
> have to shout 'bye' three times in a row. make sure to test your
> program: if you shout 'bye' three times, but not in a row, you should
> still be talking to grandma." but, i'll worry about that after i've got
> the previous stuff squared away. thanks in advance.
Here's an iterative approach to solving this:
0. Write an echo program. Ask the user for some input, repeat it back
and exit. E.g.
$ ruby grandma.rb
Please say something: Hello world
You said: Hello world
1. Set up an echo loop: ask the user for some input, then say it back,
then repeat those two steps endlessly
2. Write an expression that returns 'true' if a string is in all caps
and false otherwise
3. Have your program only echo back if the input is in all caps
4. Add the "huh? i can't hear you!" if the input is not in all caps
Look up the section titled "Branching" in
http://pine.fm/LearnToProgram/?Chapter=06 if you have trouble with
steps 3 and 4
5. Now go back to the code that says what to do if grandma *can* hear
you, and add your random number code
It might be helpful to write your program out as an algorithm first
(i.e. forget about syntax, just write out the flow of logic. For
example, this is the Mrs Gabbard code from chapter 6 written out as an
algorithm:
Introduce yourself as Mrs Gabbard
Ask the student for his/her name
See if the name is properly capitalised
---If it is
------Ask the student to take a seat
---Otherwise
------Work out the properly capitalised form
------Ask if that is what the student meant
------If the answer is 'yes'
---------Say "take a seat"
------If not
---------Say "get out!"
martin
that's the gist of my questionnaire. it's been frustrating, but as i
figure things out by myself or through the hints people give me, i get a
feeling of accomplishment. i understand that i'm just starting out & i
shouldn't expect to have an elite knack to code in ruby, but i've been
at this program on & off for 2 days & i'm not really getting all the
results i've wanted. there's still a lot i need to work in.
specifically, grandma's response if you give her initial question an
answer that is not "yes," "Yes," "no," or "No," on top of the extension
to the program, which is a whole other monster for the time being.
again, any help is appreciated.
Well. you have a whole lot of "if" statements. An if statement has a
something which is true or false; in your case
reply1 == "yes"
If it's true, the code behind it will run. If it's false, the code after
an else-statement will run. If there is no "else" , the if statement
will ignore all code until it finds it's "end" statement.
In your case that means all following code is skipped. The "end"s are
misplaced.
You might want to look into "while" or "until" to tackle this.
childs_reply = ""
until childs_reply == "BYE"
childs_reply = gets.chomp
##do smart stuff
#if
#else
#end
end
Regarding grandma's typing. She's going to do this all the time, so
let's make a method. Basically we want to recieve a string, take each
letter, print it and then pause a little.
def grandma_says(str) #str is the stuff we're going to show.
str.each_byte do |byte| #Whoa. each_byte? Never mind, it's there to
print byte.chr #handle non-western character sets.
sleep 0.1
STDOUT.flush #major pain in the ass, this (on windows).
end
puts #forces a line ending when grandma is
finished.
STDOUT.flush
end
grandma_says("I may be old but I'm not slow!")
Hth,
Siep
I looked at your code, and it looks like you're taking an inside-out
approach to the problem; that is, you're concentrating on the little
bits and then trying to fit them into some sort of overall structure.
Particularly since you're learning to code, I'd strongly recommend
leaving that code alone, and starting from scratch using a more
top-down approach. Specifically, get the *structure* of your program
correct first, then slowly fill in that structure to have all the
features you need. Go back and check out the iterative approach I
suggested in my last post, where you build up the code feature by
feature starting with the basic question-answer-question-answer....
loop.
After you've been at it a while, this will become second nature to
you, and you'll be able to keep the structure of the program in your
head and fill in details even before you have the whole skeleton
mapped out, but that takes experience, and unnecessarily complicates
matters at this stage. Also, when you come to chapter 8 in the
tutorial you'll learn about methods, which are a powerful way to keep
your program well-structured throughout its development.
martin
def type(string, speed=5)
old_sync = $stdout.sync
$stdout.sync = true
string.each_char do |c|
print c
sleep 1.0/speed
end
$stdout.sync = old_sync
end
type "foo\nbar\bz"
This will type 5 letters per second (the default value for speed), first
typing foo, then a newline, then bar and then deleting the r in bar and
replacing it with a z.
So put this method definition at the beginning of your script and then use
type instead of puts. If you're using ruby 1.8.6, you'll also have to
require 'jcode' at the top of the file.
HTH,
Sebastian
--
Jabber: sep...@jabber.org
ICQ: 205544826
I'm a bit too busy at work for realtime stuff these days - but have
you discovered the ruby IRC channel? It's #ruby-lang on
irc.freeenode.net, and there are lots of helpful, friendly people on
there.
martin
0.
puts
puts "Please say something."
puts
reply = gets.chomp
puts
puts "You said #{reply}."
1.
loop {puts "Please say something."
puts
reply = gets.chomp
puts}
2.
puts
puts "The world is flat. True or false?"
puts
reply = gets.chomp.downcase
if reply == "true"
puts
puts "You must've read Thomas L. Friedman's book!"
elsif reply == "false"
puts
puts "Ah, submitting to Pythagoras' proposal that"
puts "the world is round, I see. That's so 2,508"
puts "years ago!"
end
3. (I cannot take ownership for writing this. sepp2k from #ruby-lang
helped me considerably. He alerted me to the fact I shouldn't be using
go-to style flow control & told me looping was the better bet.)
reply = "abc"
while reply != reply.upcase
puts
puts "Simon says, 'Input your text in all"
puts "caps! Otherwise, I won't echo back"
puts "anything you say! Instead, I will"
puts "spit out my 'Simon says' spiel once"
puts "more. & you wouldn't want that!"
puts
reply = gets.chomp
end
puts reply
.. for now, that's all I've got. I've been working on each individual
step of the iterative approach & trying to pick up what I can through my
questions on #ruby-lang. I'll post the code for 4 & 5 later on in the
day. Please take a look over what I've written (with the help of an
array of people) & let me know if there's anywhere I can clean it up.
Thanks!
This is actually shaping up very well. Here's a comment:
> reply = "abc"
> while reply != reply.upcase
> puts
> puts "Simon says, 'Input your text in all"
> puts "caps! Otherwise, I won't echo back"
> puts "anything you say! Instead, I will"
> puts "spit out my 'Simon says' spiel once"
> puts "more. & you wouldn't want that!"
> puts
> reply = gets.chomp
> end
>
> puts reply
The algorithm for this is:
loop
---- get a reply
---- if the reply is not in all caps
------ repeat loop
---- otherwise
------ exit loop
You're not doing anything with the reply yet. The following skeleton
might be easier to work with
0 loop
1 ---- get a reply
2 ---- if the reply is in all caps
3 -------- do something
4 ---- otherwise
5 -------- do something else
6 ---- repeat loop
Note that steps 1-5 simply get a reply and deal with responding to it,
and that they are contained cleanly inside the loop body. Of course,
you do want to exit the loop at some point, but I'd suggest doing it
this way:
0 loop
1 ---- get a reply
2 ---- if the reply is in all caps
3 -------- do something
4 ---- otherwise
5 -------- do something else
6 ---- break out of the loop if we need to
7 ---- repeat loop (unless we've broken out)
Ruby does support the 'break' call to exit a loop, but another, and
often helpful way to do things is to have a variable act as a flag,
and check the flag each time to see whether we loop again. Then,
within the body of the loop, you signal "ready to exit" by setting the
flag. Here's some code that demonstrates both approaches:
# one way: use a flag to end the loop
done_talking = false
while (not done_talking) do
puts "Loop 1> Say something (or 'quit' to exit)"
reply = gets.chomp
if (reply.downcase == "quit")
done_talking = true
else
if (reply == reply.upcase)
puts "that was in all caps"
else
puts "that was not in all caps"
end
end
end
puts "exited loop 1"
# and the other way: loop infinitely, but break out if needed
while true do
puts "Loop 2> Say something (or 'quit' to exit)"
reply = gets.chomp
if (reply.downcase == "quit")
break
else
if (reply == reply.upcase)
puts "that was in all caps"
else
puts "that was not in all caps"
end
end
end
puts "exited loop 2"
# an advantage of the 'break' method is that it saves one level of if/else
while true do
puts "Loop 3> Say something (or 'quit' to exit)"
reply = gets.chomp
if (reply.downcase == "quit")
break
end
if (reply == reply.upcase)
puts "that was in all caps"
else
puts "that was not in all caps"
end
end
puts "exited loop 3"
>
> ... for now, that's all I've got. I've been working on each individual
> step of the iterative approach & trying to pick up what I can through my
> questions on #ruby-lang. I'll post the code for 4 & 5 later on in the
> day. Please take a look over what I've written (with the help of an
> array of people) & let me know if there's anywhere I can clean it up.
> Thanks!
No problem :) Looking forward to seeing the next two parts.
martin
I haven't looked at Pine's specific question, but I'd be a little
surprised if it's this simple...
s = some_string; s.upcase == s
Todd
BTW, that doesn't give you a direct answer to your whole question, but
may be what Pine was looking for the _shout_ part.
Todd
==========
def granny_says(str)
str.each_byte do |byte|
print byte.chr
sleep 0.05
STDOUT.flush
end
puts
STDOUT.flush
end
while true do
granny_says("Got a question for granny? Make sure to")
granny_says("input yo' question in all caps, or else")
granny_says("granny can't hear ya'!\n\n")
reply = gets.chomp
if (reply == reply.upcase)
random = rand(21) + 1930
granny_says("\nNo, not since #{random}!")
granny_says("That's all the time I've got, child!")
granny_says("Hasta la-bye-bye!")
break
else
granny_says("\nGranny can't hear you! Wanna know why?")
granny_says("'Cause that wasn't in all caps, yo'!")
granny_says("So, I'll repeat what I said before ...\n\n")
end
end
==========
Now that I've got that squared away, I'll look into adding the extension
to the exercise. & to tell you the truth, I've no idea how to do that.
I'm thinkin' I need to create another loop, & define (some how, I'm
still not sure) that Grandma will only say goodbye after the user has
inputted "bye" three times in a row. I guess I have an inkling of an
idea of how to go about it, just not sure it'll work. If all goes well,
I'll be posting later tomorrow with the total exercise, extension
included, complete. Wish me luck!
It looks good :) And as you note in your next post, the extension
doesn't really need another loop, just something within the same loop
that adds an additional check for whether to exit or not.
martin
I'd say it's about 99% complete. Although I do need the following, as
mentioned above:
• uniform indentation;
• shortened print statements;
• one less loop
If you could suggest how I go about doing any of the above, or throw
some URLs my way that speak on the topics of indentation & the like, I'd
appreciate it. Once I get your suggestions (or toy around more with the
code) & I've re-worked everything, I'd say we're golden.
This is something a good editor should do for you. If you let us know
what platform you are on, people can recommend their favourite editors
and IDEs :) I use gvim myself; below is an example of how it
autoindents your code:
A heavyweight but very nicely polished option is netbeans with ruby
support: http://wiki.netbeans.org/Ruby
> • shortened print statements;
Not sure what you mean by that. If you have a lot of text to print, it
needs to be included, after all :) There are things you could do to
compact the code but they add little value at this stage - I'd say go
ahead and do it the straightforward way as you have here.
> • one less loop
There's a problem with your code as written - note that once you've
said "bye" you can never say anything else - it sticks in the loop
till it counts three 'byes' in a row. Whereas what you want is to exit
the whole loop if you see three byes in a row, otherwise just carry on
as normal. What you *should* do is this -
if the user has yelled, so that granny hears what he says, do an
additional check for his having said "bye".
---- If that is true, then increase the bye count.
-------- If the bye count is 3, exit the loop
---- If that is false, reset the bye count to zero and carry on with
the rest of the code
Hope that helps!
martin
class Grandma
end
:)
Actually, if you listen to the problem statement, he wants
def grandma
end
:)
martin
Martin, this keeps being brought up. If I'm understanding correctly,
after the Intermediary explains that questions towards Granny should be
in all caps if you want her to hear you, you want to additionally check
the inputted text (if it's in all caps) to check if "BYE" has been said.
But, logically that doesn't make any sense. Why would the first thing
you say to Granny, after it's been suggested you ask a question, be
"BYE"? It doesn't make sense to me.. which is why I believe I put the
loop where it lays currently. Of course, I could be completely
misunderstanding what you're saying. Which is more likely! If I am, just
clarify & I'll begin working on what you've been trying to say. At it's
written, it works as I want it to. It is still heavily rooted in Chris
Pine's exercise & covers most, if not all, of the requirements. The one
thing that I believe is most significant is the loop issue, which I hope
to have sorted out later today if all goes well.
>If you let us know what platform you are on, people can recommend their >favourite editors and IDEs
I'm on Windows XP. Recommend away!
What you're missing is that, because it's a loop, the same code gets
executed for every question. Consider the following code
1 loop do
2 granny_says("<granny> say something")
3 reply = gets.chomp
4 if (reply == reply.upcase)
5 granny_says("<granny> i hear you loud and clear")
6 else
7 granny_says("<granny> eh? speak up!!")
8 end
9 end
Now consider the transcript
<granny> say something
hello
<granny> eh? speak up!!
<granny> say something
i said "hello"
<granny> eh? speak up!!
<granny> say something
"hello"!!!!!!!
<granny> eh? speak up!!
<granny> say something
HELLO!
<granny> i hear you loud and clear
<granny> say something
Here it is again, annotated with the line of code being run
1
2 <granny> say something
3 hello
4
6
7 <granny> eh? speak up!!
2 <granny> say something
3 i said "hello"
4
6
7 <granny> eh? speak up!!
2 <granny> say something
3 "hello"!!!!!!!
4
6
7 <granny> eh? speak up!!
2 <granny> say something
3 HELLO!
4
5 <granny> i hear you loud and clear
2 <granny> say something
Now if the user wants to say 'bye', which lines of code will handle
it? Note that the only place where the program prompts for user input
is lines 2 and 3. So we run line 2
2 <granny> say something
and wait for user input
3 BYE
the program will go to line 4, then to line 5, then skip 6,7,8 (the
else block) and go to line 9 where it loops again. So the place to
process the user's input and check if he has said "bye" is inside the
if block. Ideally, instead of
-- say "i hear you loud and clear"
you want
-- did he say "bye"?
---- was it the third time in a row?
------- if so, exit
------- if not, pretend you didn't hear
so you want (1) an inner if/else block to replace line 5 and (2) logic
within that block to keep track of whether we've seen three
consecutive 'bye's
The problem with using a separate loop is this:
<granny> say something
"hello"!!!!!!!
<granny> eh? speak up!!
<granny> say something
HELLO!
<granny> i hear you loud and clear
<granny> say something
BYE
<granny> i'm afraid i didn't hear you properly - it almost sounded
like you said 'bye'
<granny> say something
okay, I'm not leaving
Now what? If you're inside a "wait for three byes" loop, you have no
way of handling something that is *not* a BYE. So you need to have a
single loop whose overall structure is
loop do
-- ask for input
-- do something with input
-- check if we need to exit
---- if so, exit
---- if not, make a response
end
And use variables for any tracking you need to do that spans multiple
repeats of the loop
>>If you let us know what platform you are on, people can recommend their >favourite editors and IDEs
>
> I'm on Windows XP. Recommend away!
I'm on linux, so no real recommendations, I'm afraid. See if
http://rubyonwindows.blogspot.com/2007/05/what-text-editor-should-i-use.html
has anything helpful to suggest. Also, people's requirements vary; for
me, autoindentation is the key feature of a good programming editor,
followed by decent syntax highlighting.
martin
This is what i finally came up with hope it helps;
count = 1
while count != 4
response = gets.chomp
if response == 'BYE'
count = count + 1
else
count = 1
end
if response == response.upcase && response != 'BYE'
puts 'Just like in ' + (rand(30) + 1920).to_s
end
if response == response.downcase
puts 'HUH?! SPEAK UP, SONNY!'
end
end
Finally! After 8 long months of checking this thread every day, someone
posts a solution. Thank you, thank you, thank you.
Sorry for the interuption but you may find the RubyLearning Free online
course useful.
3 months duration no fee only your time and commitment to join
discussions, try writing programs and call for help when stuck.
url is www.rubylearning.com
sorry to those who i might offend
dave.