From: Ben Brightwell Date: 2008-07-29T14:03:26+09:00 Subject: Re: Another Deaf Grandma Topic, different issue 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-forum.com/.