[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Another Deaf Grandma Topic, different issue

Phee Luche

7/29/2008 4:52:00 AM

I'm trying to learn using a tut, and I'm getting stuck on one of the
exercises. Google is giving me squat.

It's about Flow Control, specifically while and if-then. I understand it
totally, but I need, well, it's hard to explain.

I've successfully finished this:

Write a Deaf Grandma program. Whatever you say to grandma (whatever you
type in), she should respond with HUH?! SPEAK UP, SONNY!, unless you
shout it (type in all capitals). If you shout, she can hear you (or at
least she thinks so) and yells back, NO, NOT SINCE 1938! To make your
program really believable, have grandma shout a different year each
time; maybe any year at random between 1930 and 1950. (This part is
optional, and would be much easier if you read the section on Ruby's
random number generator at the end of the methods chapter.) You can't
stop talking to grandma until you shout BYE.
Hint: Don't forget about chomp! 'BYE'with an Enter is not the same as
'BYE' without one!
Hint 2: Try to think about what parts of your program should happen over
and over again. All of those should be in your while loop.[/spoiler]

But now I need to do a bit more, and I can't figure out how to do this:

Extend your Deaf Grandma program: 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.

Oh, and here's my code:

puts 'it\'s yer ol\' gramma hobblin\' over! What do you say?'
you=gets.chomp


while you!='BYE'
if you!=you.upcase.chomp
then
puts 'HUH?! SPEAK UP, SONNY!'
you=gets.chomp
else
if you==you.upcase.chomp
then
puts 'NO, NOT SINCE 19'+(rand(21)+30).to_s+'!'
you=gets.chomp
end
end
end
--
Posted via http://www.ruby-....

6 Answers

Ben Brightwell

7/29/2008 5:03:00 AM

0

Phee Luche wrote:
> I'm trying to learn using a tut, and I'm getting stuck on one of the
> exercises. Google is giving me squat.
>
> It's about Flow Control, specifically while and if-then. I understand it
> totally, but I need, well, it's hard to explain.
>
> I've successfully finished this:
>
> Write a Deaf Grandma program. Whatever you say to grandma (whatever you
> type in), she should respond with HUH?! SPEAK UP, SONNY!, unless you
> shout it (type in all capitals). If you shout, she can hear you (or at
> least she thinks so) and yells back, NO, NOT SINCE 1938! To make your
> program really believable, have grandma shout a different year each
> time; maybe any year at random between 1930 and 1950. (This part is
> optional, and would be much easier if you read the section on Ruby's
> random number generator at the end of the methods chapter.) You can't
> stop talking to grandma until you shout BYE.
> Hint: Don't forget about chomp! 'BYE'with an Enter is not the same as
> 'BYE' without one!
> Hint 2: Try to think about what parts of your program should happen over
> and over again. All of those should be in your while loop.[/spoiler]
>
> But now I need to do a bit more, and I can't figure out how to do this:
>
> Extend your Deaf Grandma program: 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.
>
> Oh, and here's my code:
>
> puts 'it\'s yer ol\' gramma hobblin\' over! What do you say?'
> you=gets.chomp
>
>
> while you!='BYE'
> if you!=you.upcase.chomp
> then
> puts 'HUH?! SPEAK UP, SONNY!'
> you=gets.chomp
> else
> if you==you.upcase.chomp
> then
> puts 'NO, NOT SINCE 19'+(rand(21)+30).to_s+'!'
> you=gets.chomp
> end
> end
> end

Phee, in the expression for your while loop ( you != 'BYE' ), your
program will exit after precisely 1 'BYE' entered by the user. This
should be the first place you look, since you don't want your program to
exit until there are a total of three 'BYE' entries in a row.

My first thought would be to keep a count of how many times the user has
input 'BYE' and check that against 3 ( while bye_count != 3 ).

Hope that helps.
--
Posted via http://www.ruby-....

Phee Luche

7/29/2008 5:15:00 AM

0

Ben Brightwell wrote:
>
> Phee, in the expression for your while loop ( you != 'BYE' ), your
> program will exit after precisely 1 'BYE' entered by the user. This
> should be the first place you look, since you don't want your program to
> exit until there are a total of three 'BYE' entries in a row.
>
> My first thought would be to keep a count of how many times the user has
> input 'BYE' and check that against 3 ( while bye_count != 3 ).
>
> Hope that helps.

I haven't yet learned count, and so I'm assuming that there's another
way to do this. I understand the concept, but how do I define bye?
--
Posted via http://www.ruby-....

Martin DeMello

7/29/2008 5:24:00 AM

0

On Mon, Jul 28, 2008 at 10:15 PM, Phee Luche <phee92@gmail.com> wrote:
>
> I haven't yet learned count, and so I'm assuming that there's another
> way to do this. I understand the concept, but how do I define bye?

Just have a variable called bye_count that contains a number

bye_count = 0
puts bye_count
bye_count = bye_count + 1
puts bye_count

martin

Phee Luche

7/29/2008 6:10:00 AM

0

Thank you so much!

I renamed it and reworked everything to fit it in.

If you care to take a look, here is my code, reworked. It (seems to, at
least) work perfectly. :)

you = gets.chomp
byes = 0

while byes != 2
if you=='BYE'
then byes = byes + 1
else
byes=0
end
if you != you.chomp.upcase
then
puts 'HUH?! SPEAK UP, SONNY!'
end
if you == you.chomp.upcase
then
puts 'NO, NOT SINCE 19'+(rand(21)+30).to_s+'!'
end
you = gets.chomp
end
--
Posted via http://www.ruby-....

Simon Anker

7/30/2008 11:59:00 PM

0

I think I've spent maybe two hours or more solving this, but I finally
got it in the end.. I hope so anyway :) .. I have done a bit of a Python
tutorial a bit ago, so I think I have a bit of more experience than
Phee, but not much! So don't yell at me! :)

Phee, try inputting "BYE", "BYE", "test". I made the same mistake a
bunch of times.

Also your indenting is a bit off. Look at the example in the "A Little
Bit of Logic" section of Chapter 6 to get it right.

I've edited your second program to look nicer, comments below:

you = gets.chomp
byes = 0

while byes != 2
if you=='BYE'
byes += 1
else
byes=0
end
if you != you.upcase
puts 'HUH?! SPEAK UP, SONNY!'
else
puts 'NO, NOT SINCE 19'+(rand(21)+30).to_s+'!'
end
you = gets.chomp
end


"byes += 1" is just another way of writing "byes = byes + 1". This
haven't been stated any where in the tutorial though. Nor have the count
concept as far as I know, but I don't see how to do the exercise without
it, unless you do it with rather ugly code.

I've removed all the "then" statements as they are unnecessary. I don't
know where you picked those up, as I haven't seen them in the tutorial
;O .. I assume we're doing the same one (I'm doing this:
http://pine.fm/Learn...).

Anything that isn't "!= you.chomp.upcase" IS "== you.chomp.upcase", so I
could remove that third "if" question as well. Think of it like two
negatives making it positive (1 - (-1) = 2).

You don't really need those .chomp's in the while loop when you do it
when you get the string (gets). I guess they can be helpful if someone
forgets to put a chomp on the end of gets, or you only need the string
to be chomp'ed occasionally, but that isn't the case here.


What I did to solve it was to make the while loop run until the program
turned it off, like this (example could be a very personalised music
player on your pc):

keepPlaying = 1

while keepPlaying != 0
if
music_I_like_is_playing == True
else
keepPlaying = 0
end
end

Basicly an on/off switch.
--
Posted via http://www.ruby-....

Simon Anker

7/31/2008 12:20:00 AM

0

Actually, after thinking about it a bit more, the on/off switch isn't
necesary, as that's what the while loop is for. I don't think I've used
an if question without an else question before today, so didn't think
about it that way. I think that such an on/off switch can be useful in
some cases (no?), but in this case it wasn't.
--
Posted via http://www.ruby-....