Daniel Hollocher
7/19/2006 1:52:00 AM
My suggestion would be to add that behavior right when you are getting
the user input. Add a check at the beggining, and keep it out if the
if statement.
Also, I would change the if/elsif statements to a case statement. The
case statement is perfect for what you are trying to do.
Dan
On 7/18/06, simonh <simonharrison@fastmail.co.uk> wrote:
> I posted a question a week or so now which I got some great help with.
> Here I am again I'm afraid! What I am now struggling with is how to add
> help. What I want is if the user gets the input wrong three times, I'd
> like to print to print
>
> > Press 'h' for help
> > h
> > You must enter your age as two digits. i.e if you are twenty five, type 25.
>
>
> here is the original method:
> -----------------------------------------------
> def check_age
> print "Please enter your age: "
> age = gets.chomp
> target = 18..30
> valid = /^\d{2}/
>
> if valid.match(age)
> if target === age.to_i
> puts 'Have a nice holiday!'
> elsif age.to_i < 18
> puts 'Sorry, too young.'
> elsif age.to_i > 30
> puts 'Sorry, too old.'
> end
> else
> puts 'Incorrect input.'
> puts
> check_age # Try again
> end
> end
>
> check_age
> gets
> ---------------------------------------------------
>
> Should I create a class and add a 'show_help' method (putting the
> 'check_age' method in there as well)? If so, how/where do I call that
> method? The method in the above code calls itself. I am a complete
> programming newbie by the way.
>
> Maybe we could expand this program to help other new programmers like
> myself? The jukebox example in Pickaxe2 seems to fizzle out.
>
>
>