Friday, June 5, 2015

Module 3 - Python Fundamentals Part 2

This week in GISProgramming we learned a few more fundamentals of scripting in Python using PythonWin. We were challenged to import a module, fix errors in an existing script, and write our own basic scripts using conditional statements and loops. In the weekly exercises we got practice doing all of these tasks and then for the lab we were given some general guidance to follow and several end-tasks to meet.

The first section of the lab required importing the random module in order to use some of the random functions later on in the lab. This step was pretty simple, just two words in one line of code. The next section required us to fix two errors in an existing script. During this section I used the help section of the Python window in ArcGIS to recognize discrepancies with some of the functions. I also remembered that Python is case sensitive (because I've been using all lowercase letters so far) which cleared up one of the errors. Once the errors were fixed and i ran the script the results were a simulated dice-like game using the player's names. The names and win/loss statements in the graphic below are the results of the game.
Module 3 Dice Game and Number List
The third and fourth parts of the lab went together. The third section required me to create a loop that would produce a random list of 20 numbers with values ranging 0-10. I used a list, the random range function, a while loop, the len function, and the count method to accomplish this. I learned that the range needed to be one number higher than 10 in order to actually include 10 in the random number selection. I ran the script a bunch of times waiting for a 10 before I figured this out. In the fourth section of the lab we had to remove an unlucky number from the list. In addition to number removal we also had to print a statement indicating if the list did or did not contain the unlucky number and if it did, the statement needed to indicate how many times was the unlucky number present. To complete this task I created a variable to represent the unlucky number. Then I used the variable in a conditional if/else branching structure and wrote out the two sentences that I wanted printed. To count the amount of unlucky numbers I used the count method and added it as a string function to one of my printed statements. To remove the unlucky numbers I used a while loop and the remove method. The results are pictured at the bottom of the graphic above. The initial random list of 20 numbers printed, a statement noting how many times the unlucky number occurred printed, and the new list with the unlucky number removed printed. The lab took me longer than it should have because I realized I was trying to make things a lot more complicated than they needed to be.

In addition to the normal coursework I am also t the point where I need to start thinking about what I want to do for my end of semester project and I have no idea. I will add a note next week about the progress I make with at least deciding on an idea. 

No comments:

Post a Comment