[Tutor] Is this code right?

Wesley J. Chun wesc@alpha.ece.ucsb.edu
Thu, 30 Mar 2000 15:49:18 -0800 (PST)

    > From: "christian lean" <christianlean@hotmail.com>
    > Date: Thu, 30 Mar 2000 23:20:53 GMT
    > I was wondering if this code is right i ran idle's debug on it and
    > no errors came up but i cant seem to use the function when i import
    > the module it is in. The function is meant to create a list of random
    > numbers, im new at this so i dont know if i'm doing something wrong.
    > Any help is greatly apprietiated. I'm running Win 98 and Python 1.5.2.
    > import whrandom
    > def rand_strng(x, y, z):
    >     while x > 0:
    >         w = randint(y, z)
    >         list[x] = w
    >         x = x - 1
    >         return x
    >     return list

syntactically, the code is right, but what are you trying to do?
it looks like you are trying to stuff a set of random numbers
into a list backwards.

there are numerous logical problems with the code though:

1.  the biggest problem in your code is the presence of two return
2.  the first return (return x) is guaranteed to return a zero
    when the list is done (you also lost your list)... probably
    not what you are looking for.

3.  the second return will result in a NameError because you are
    accessing an uninitialized local variable (list).  this return
    statement will be called if x == 0 or x < 0, also something
    you are probably not shooting for.

4.  it is probably not a good idea to use a built-in variable
    name as the name of a local variable.  "list" is the name
    for the built-in list() function, which you will override
    if you use it.

5.  unless a list called 'list' exists as a global variable,
    your "list[x] = w" statement will fail because you are
    changing existing list elements as opposed to creating
    a new list and adding elements to it.

6.  you are not calling randint() correctly.  you need to either
    bring it into your namespace to call it directly (use from-
    import), or give the full attribute name (whrandom.randint).

other issues:

7.  why are you counting backwards?

8.  you might want to improve the naming of your variables.
    in this relatively short example, we can figure out what
    x, y, and z are, but if your application grows in size,
    these may not be the best choices for easy readibility.

below is a shorter solution.  the main differences are:

a. counting forwards
b. creating a new list and appending values to it
c. returning the new list instead of x

- - - - - - - - - - - - - - - - - - - - - - - - 
import whrandom

def rand_strng(x, y, z):
    randlist = []
    for eachVal in range(x):
	w = whrandom.randint(y, z)
    return randlist
- - - - - - - - - - - - - - - - - - - - - - - - 

you can also remove the assignment to w and
add the random number directly to the list with:

    randlist.append(whrandom.randint(y, z))

this just saves the memory of one local variable,
not necessary, but since w is not used for any-
thing else, may make sense.

hope this helps!!


"Core Python Programming", Prentice-Hall, TBP Summer 2000

wesley.j.chun :: wesc@alpha.ece.ucsb.edu
cyberweb.consulting :: silicon.valley, ca