In our first week of GIS Programming, I learned the foundations of Python. Wow, was this incredibly hard! I found it extremely challenging to wrap my head around the subtle nuances of syntax and all the different functions and methods available to use. I love puzzles and critical thinking, and that is a huge part of python; coming up with efficient and creative solutions. As I become knowledgeable and better at using python, I am excited to see all the ways I will be able to use it.
Our Lab this week consisted first of learning how to create a string and then indexing or slicing them. This was the easiest of the tasks, as we learned also to use a while loop in combination with functions(i.e.: range, len, append, random) and methods (i.e.: object.method(argument)). Utilizing the while loop, I debugged a prewritten code that generated random wins and losses for players in a dice game. I also created/utilized a while loop to generate 20 random numbers between 0 and 10. Then I chose an unlucky integer and then created an if-else statement to count the number of times said integer repeated in the generated list. Lastly, I wrote another while loop to remove said integer anytime it repeated in the generated list.
I was stuck on the last couple of steps trying to figure out what the object representation was for the generated list, so I could do list.count(“num”) to count how many times my chosen integer appeared. My issue was that I forgot to append the list: list.append(n) [where n is the randomly selected numbers]. I then created a if-else statement to print out the list. I also made the list an integer (y) to condense the statement.
Lastly, I had a hard time removing the unlucky numbers. After a lot of research and some help from classmates on the discussion board, I realized that I had to add the methods len and range. I was originally using a too simple while loop code without any functions. This resulted in the same list of 20 generated numbers as the previous step, while I desired to remove the chosen integer from the list. I had failed to see that I needed to reference the new desired length of the list and the range.
Below, is the outcome of the final script.
Comments
Post a Comment