Another Deaf Grandma Topic, different issue

P

Phee Luche

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
 
B

Ben Brightwell

Phee said:
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.
 
P

Phee Luche

Ben said:
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?
 
M

Martin DeMello

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
 
P

Phee Luche

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
 
S

Simon Anker

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/LearnToProgram/).

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.
 
S

Simon Anker

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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top