[Tutor] newbie looking for code suggestions

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 22 Sep 2002 20:21:29 -0700 (PDT)


On Sun, 22 Sep 2002, Bob Roher wrote:

> Hi all.  I was wondering if anyone could look over the following code
> and give me any suggestions or criticisms?  I just started learning
> Python a few days ago, so I'm sure there's a lot that can be done to
> make it look and run better.

No problem; let's take a look at parts of your code.



> #User enters parameters for min, max, and what I call digit total for
> #lack of a better word.
> while run_again == "y":
>     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: ")


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, I'm wussing out there.  *grin* I haven't written a real definition
that takes max_number and min_number into account, but at least it'll work
for now.




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.



Good luck!