Programming Week 2: Python Fundamentals

In this week's module, we practiced writing and editing a simple Python script in four steps. In Step 1 we wrote our full name as a string, then split each name into separate items in a list, then selected our last name using an index. In Step 2, we had to find and correct errors in code for a dice game in which each player rolls dice with a total value that is twice the number of letters in their name, and they win if their roll is greater than half the possible total. 

In Step 3, we wrote a while loop with an if statement and break statement that created a list of 20 random numbers between 0 and 10. I was not sure what a counter variable was, so I had to look that up. I was able to write the code for Step 3 easily as just a while loop, but once I added an if statement and a break statement, I got stuck in an infinite loop and had to terminate ArcGIS Pro. I think it was because I originally included print statements in my while loop, although I don’t understand why that would create an infinite loop. The code without an if statement or break statement is shorter and seems more elegant to me, and I’m a little unsure of when a break statement is necessary. 

In Step 4, we chose an "unlucky" number that we had to remove from the list of randomly generated numbers (luckyList). I created a while loop that runs as long as the chosen number is still in luckyList. Using the remove method removes the first occurrence of the chosen number in luckyList. The code then loops and continues to remove the next instance (the new first instance of that number) until there are no more instances of that number in the list, at which point the loop automatically ends. 

The outputs for all 4 of these steps is below:



Comments

Popular posts from this blog

Week 4: Vector Analysis

Remote Sensing Final Project: Change in NDVI Over Time