Friday, June 21, 2019

Bogus Python Code for Dummies

Python IDE
Python IDE
One of our staffers was wandering around in the depths of the Leaf Group niche site Techwalla.com the other day. Techwalla is, we think, the oldest niche at Leaf, not that that makes any difference: it's still chock full of rubbish, since all of it came from eHow.com. The kid ran across a post about coding and, nonplussed, brought it up at the next day's nomination meeting. It won. Feast your eyes on G. S. Jackson and his stab at "How to Generate Odd Numbers in Python."

Like many a homework question attacked by an eHowian over the years, this one didn't make much sense to our staffers. Why would anyone want to do that, anyway? G. S., however, had to come up with an answer to that question for his introduction, so he said that,
"...a programmer may want to build lists of specific, recurring patterns of numbers. Or, rather, a programmer wishes to use a number generation algorithm as an input function."
Either of which is a bullshit reason, but what the heck. We'll run with it. What we won't run with, however, is the code that appears in Jackson's post. According to G. S., you perform all this work in the IDE (Python's Integrated DeveLopment Environment, sometimes known as IDLE¹). We guess that's OK, although we figure IDLE is better for testing code than writing it... so that's what we did.

According to the post, Jackson wants you to enter the following code into IDLE:

i=0 >>>while i <10:
i=10 >>>while i < 10: ... if i % 2 != 0: ... print i ... i = i + 1

The staffer most familiar with Python looked at the code, scratched his head, and said, "That's not Python code!" And then he thought about it for a while and said, "Looks like eHow screwed it up and G. S. didn't bother to correct them. What we think Jackson actually wrote would was,

i=0
while i <10:
      if i % 2 != 0:
            print i
      i = i + 1

But the port from eHow to Techwalla hosed the formatting. Of course, G. S. didn't really need to add the three > symbols that are the IDLE prompt – that didn't help much, we are sure. Then again, we aren't sure why Jackson chose a while loop instead of a for loop, but that's probably a matter of choice. What we do think he screwed up is that it's unlikely that someone wants the first few odd integers: they probably want a random set of integers (unless this is truly a beginning Python assignment).
For that, we'd use the built-in pseudorandom number generator to dump out a few odd number in, say, the range 0 to 999:

import random
for a in range(0,10):
    i = random.randint(0,999)
    if i%2 == 0:
        i+=1
    print i

We'd like to think Jackson's not the Dumbass of the Day, but surely he could have done better (and Techwalla should be ashamed of itself!)


¹ As in Eric Idle, founding member of Monty Python.
copyright © 2019-2021 scmrak

DD - CODING

No comments: