[Tutor] Stuck on Error

Keith Winston keithwins at gmail.com
Sun Dec 22 10:39:24 CET 2013


On Sun, Dec 22, 2013 at 3:55 AM, <tutor-request at python.org> wrote:

> import random
>
> for i in range(1):
>     RN1 = random.randint(1,75)
>

As noted before, these "for i in range(1)" statements are pointless:
iteration over a range of 1 is no iteration at all. This is exactly
equivalent to simply saying

    RN1 = random.randint(1,75)

One thing you might want to do is carefully document your code: add a bunch
of lines, preceded by the pound sign # (to make them into comments), to
explain what you're trying to do in the next section. This might help you
clarify your thinking, and it will definitely help others understand your
intention. Like this:

# Collect a set of lottery results
RN1 = random.randint(1, 75)
RN2 = random.randint(1, 75)
etc.

You could do this entire piece with a list comprehension in one line, but
I'm only mentioning it b/c I just learned them. My crude first effort would
look like this:

RN = []  # create the array RN
[RN.append(random.randint(1, 75)) for i in range(5)]  # populate the array

Also, the fact that you have an identical set of assignments twice makes
one wonder if it's time for a function...

Mostly though, add comments!
-- 
Keith
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131222/107eda2a/attachment-0001.html>


More information about the Tutor mailing list