Friday, June 13, 2014

GIS Programming Module 4 - Python Fundamentals Part 2

This week we learned more about Python scripting, especially conditional statements using IF and WHILE.  We also did more with lists and started working with modules that must be imported into PythonWin.

The results of the scripting assignment are shown here.  The first block is a virtual dice-rolling game that generates random numbers using the imported RANDOM module.  We only had to correct a couple of errors in the existing script.  In the second block, we generated a list of 20 random numbers, again using the RANDOM module, and a WHILE loop and a counting variable.  Finally, in the last block, we took that list of random numbers and removed all instances of an "unlucky" number from it.  For this, I used the remove method, inside a WHILE conditional loop, with IN and NOT IN operators.
 In addition, I imported the SYS method to Python and used it to allow the user to enter their own unlucky number into the Run Script dialogue box before running the script..  This was discussed in chapter 4 of our text and is very straightforward and convenient way to get input from the user.

Here is a portion of my process in this lab:

Part 1, Step 4: Removing numbers from the list. Explain how you created this block of code.

1.      I used in and not in within an if, else conditional structure to determine if the unlucky number was there or not.
2.      After the else, I used a new while loop containing the remove method: 
while unlucky in list:
list.remove (unlucky)

This ran the code until the in condition no longer applied in the while loop.

No comments:

Post a Comment