[Tutor] newbie looking for code suggestions

Bob Roher shiska@swbell.net
Mon, 23 Sep 2002 00:24:09 -0500


> Since the comment says what this code will do, perhaps it might
be nice to
> package it up into a function.  Let's call it
"get_user_input()":
>
> ###
> def get_user_input():
>     min_number = raw_input("Enter a start integer: ")
>     max_number = raw_input("Enter a stop integer: ")
>     digit_total = raw_input("What digit total would you like:
")
>     return min_number, max_number, digit_total
> ###
>
>
>
> Once we have something like this, the previous code will look
like:
>
> ###
> while run_again == "y":
>     min_number, max_number, digit_total = get_user_input()
> ###
>
>
> > One thing I would like to do is be able to have the user
re-enter
> > digit_total if they enter one that is too big without
re-running the
> > program.
>
> Hmmm... ok, since get_user_input() is now separate from the
rest of the
> program, we can just focus on getting get_user_input() to know
about this
> restriction.  How do we know what the largest possible digit
total there
> is?
>
>
> I see that you're calculating it by finding the maximum number
out of the
> number range; it might just be easier to take the maximum
outright and use
> that.  Let's pretend we had a function that, given the lower
and upper
> ranges, tells us what the highest possible digit total can be:
>
> ###
> def maximum_digit_total(min_number, max_number):
>     ## fix me!  We should really look at min_number and
max_number here.
>     return 9*6
> ###
>
>
> Ok, say we now have this maximum_digit_total() function: we can
go back to
> get_user_input() and use it!
>
> ###
> def get_user_input():
>     min_number = raw_input("Enter a start integer: ")
>     max_number = raw_input("Enter a stop integer: ")
>     while 1:
>         digit_total = raw_input("What digit total would you
like: ")
>         if it's a good digit_total:    ## <--- pseudocode
>             break
>         else:
>             print "That's a bad number.  Shame on you."
>     return min_number, max_number, digit_total
> ###
>
>
> Since get_user_input() is in a function, that's the only piece
you'll need
> to concentrate on to get that digit_total checking logic to
work.
>
>
> I hope some of this made sense!  Please feel free to ask more
questions
> about the fuzzy parts, and we can talk more about the problem.
>

Thanks Danny, that helps some.  I still have a problem with that
maximum_digit_total, though.  Can you explain what you meant for
that?  I thought I was being pretty slick when I put in a
function that would add all the digits for the upper limit and
use that as a digit total, but my wife tested the program for me
and put in a range from 1 to 200. Of course, you can have a
higher digit total than 2 in that range.  So, that's why I put it
in the loop, since the only way I could think to calculate it was
a simple compare and replace type of deal and I didn't want to
have to run through a million number sequence twice.

As for your version using statistics, that was pretty nice, too.
I considered going that route, but I really hated statistics in
school and pretty much worked my rear off for an A and tried to
forget the semester ever happened.  It would have taken me a
month to relearn it enough to write the program that way.

(Please tell me if this text isn't wrapping correctly, I'm having
fits with my email program.)