Module 2: Python Fundamentals
This weeks module was focused on the fundamentals of how to use Python within ArcGIS Pro. The focus was on using strings or text to write our names, fixing code that was previously written, writing loop statements, and removing numbers from previous generated code.
When doing string code it is important to put quotation marks around the words that you want to print. Also, when using string we tried indexing, where is the system of numbering a sequence of characters and can be used to fetch elements within a string. we are able to do this either through words in double quotation marks or lists with single quotation marks. Lists can be either be text or numbers. Lastly, I needed to just get my last name out of the list which is splicing. For this code the best way was to put -1 to get the last name out.
Next part of the module was fixing code that was already given to us. There were two errors that needed to be fixed and to start you want to run the code to see where the first problem was located. The first part that came up was that randint did not have the correct input. This code is considered a conditional statement and before it can run you need to import random before using random.randint. The second error was in the print portion of the code. It said that you cannot have both integers and strings in the print portion. So I needed to either make them all string or all integers. I had to do all string because the last portion could not be an integer. I had to put the integers in str() to get the code to work properly. After fixing that portion of the code it worked and showed the statement for each of the players.
The next portion of the code was creating a while loop statement. We were supposed to create a loop that creates 20 random numbers between 0-10. This code is similar to the one that was given to us but we needed to make sure the statement was empty and that the counter is 0. The while loop is based off a condition that was given to the code which was less than 20. Once again I needed to import random so that I could create random.randint (0, 10). In order to get the random numbers printed I needed to use the code .append(). Also, to make sure the code does not do it continuously and actually produces the numbers you need to add break before the print portion of the code.
Lastly, the last portion of the code was to chose a number that was going to be removed from the previous list that was generated. I used a for loop statement so I could get the if-else statement to determine if the number was in the generated list. Lastly, in this portion I had to use .remove() to get the number taken out of the generated list. It is important to note that the numbers are taken out of the list is done one at a time. The picture below shows the answers for all the code that was written.
Throughout all the different portions of the code there were some things that I learned which were make sure before running the loop structures code that you have a stop (break) statement. I did not have that and it caused ArcGIS Pro to crash and I had to close out of everything before I could get the code to run again. Also, make sure to do the exercise before anything and take notes that you can reference when doing the module. When doing steps 3 and 4 I needed to make sure that step 4 linked back to step 3 so that the numbers were connected. Lastly, when the numbers are being removed it is done one at a time. I think if you wanted all the numbers to be taken away at once it would be good to use the delete function.


No comments:
Post a Comment