GIS 5103 - Module 2
Introduction and Brief Overview of Module 2
In this module, 4 scripts are written, focusing on while loops and lists. The first script pulls my last name from a string of my name, making my name into a list, and then pulling my last name from the list. The second script runs a dice game in which players 'roll' dice and the outcome is a random name constrained by the number of letters in their first name. The third script runs a list of 20 random numbers between 0 and 10. And the last script identifies if there is an unlucky number in that list, and if so, removes it.
My experience with this module and lab
Throughout the process of developing and debugging these, I ran into some error messages that largely pointed me in the direction of where in the code my error was, either by highlighting the line with the error and giving a brief statement, or by running the code and getting a more detailed statement in the output as to what word or what error was occurring. This helped me identify and fix the problems. A lot of the errors I encountered were syntax errors, but occasionally the error would require tracking back and finding where a larger problem may have occurred, or I may even have been using the functions improperly.To restate from my last blog post, this is an entirely new tool for me to be using, and I am finding that while there is a definite structure to this language, it is also logic based, and thinking through the problems to find the answer takes a little bit of time for me. Additionally, some parts of the language are unclear to me still, such as which words are recognized by the program, and which need clear definitions.
Above are the results of the four scripts.
Briefly and generally, the processes for each scripts are defined below. For more detail, please see Process Summary:
Script 1: Identify variable of name. Convert name to list items. Select list item to display, last item in the list which is my last name.
Script 2: Debugging this prewritten script involved correcting some syntax errors, quick typos, and converting an integer to a string.
Script 3: Create empty list, identify count of values in list, define numbers to be random and between 0 an 10, generate values, append values to list, break list.
Script 4: Identify unlucky number, count how often unlucky number appears and define that count as a variable, write statements for print, remove values from list using remove function for when count is greater than zero, reprint new list.
Process Summary
Step 1: Printing last name. In
this step I worked in a separate workbook and then moved the code over, because
the dice game code was interfering with the process. I first defined my name by typing stringName =
‘Emily McRae’. Then I split the name using stringName.split(“ “), as was done
in the example workflow. To test, I added the print function, as in
print(stringName.split(“ “)) to confirm that the list was working. The, I
assigned this result to a variable, which I names list. Finally, I did a print
function for my list name out of the list, which was print(list[1]), to print
the 2nd word in the list. I then brought the code over to the
Mod2_Template script workspace.
Step 2: Dice Game. In this
code, there are 2 errors showing which exclamation marks on the left panel, so
I corrected those first by adding the import random function, which was the
first error because random was not defined. The second error was that X was not
defined, but a lower-case x was defined. I corrected this by making the capital
X into a lower-case x. Upon running the
script after these corrections, I still got a return error saying, ‘must be
str, not int’. The defined integer in this script was dice, as it is defined by
randint. So leaving that line of code that defines dice as an integer alone, I
changed the second appearance of the word dice, which is in the following line
in the print function to a string by changing dice to str(dice). This corrected
all errors and the code ran.
Step 3: Creating list of
random numbers. The first step in this is to define an empty list, named luckyList.
To do this, you just define the variable luckyList=[], empty brackets which
indicate a list with no values. Next, I define the counter variable, which is a
starting increment, or the count of numbers to start at when generating
numbers. This variable I am calling step. So we start with one, so the program
will make one number. Similar to step 2, you must import random, to get Python
to recognize random variables. Then, I made a while loop statement, stating
while step <= 20, number = random.randin(0,10) meaning the number in the
list is a random integer between 0 and 10. Then I wrote an append statement, so
luckyList.append(number). This appends those 20 random numbers generated to
luckyList. Then I wrote step+=1, so you keep repeating this process adding one
more random number to the count of numbers, until it reaches 20 values, as
called for earlier in the while statement. These lines made the code work, and
the while step <= 20 was sufficient to break the loop. But as specified in
the instructions, there are other ways to break the loop and those were
incorporated as well. An if statement was added at the end, if
len(luckyList)==20: break. This tells the program to break the list, to stop
generating new numbers, once the length of luckyList is equal to 20.
Step 4: Remove unlucky number
from luckyList. This step, unlike the previous three steps, was written
directly in the Mod2_Template.py page, because it depended on previous code
from Step 3 to run. My unlucky variable is just called unlucky, and is equal to
7. I have an if statement, that if unluck in luckyList:, meaning if the unlucky
number, as previously defined, is present in luckyList, perform the following.
Then a count statement, count = luckyList.count(unlucky). This tells Python to
count the number of times the variable appears in the list. Then a print
statement, in parenthesis, three quoted statements added together with plus
signs, with the count converted to a string. This makes the sentence, Unlucky
number 7 will be removed from the list x time(s). This statement I had to look
at for a bit because I forgot to add the + signs, and was trying to write it
all in one parenthesis. This is followed by an else statement to address the
case that there is no 7 in the list. The else statement reads, else: count = 0
print(‘No unlucky numbers!’). This says if the count is 0, print that sentence.
It is notable to mention that I had named my counter variable in step 3
‘count’, and had to rename it to ‘step’, because I was getting an error and I
believe it may be because the count variable was being redefined throughout and
not adequately defined when the count was not 0. My error was with the remove
statement, and this was corrected once I renamed the first count variable in
step 3. The next step involved writing a while statement to remove those
unlucky numbers from the list by writing while count > 0,
luckList,remove(unlucky), count = luckyList.count(unlucky). So here, while the
count of unlucky is greater thatn 0, the remove function removed unlucky from
the list, and then redefines the count variable. The last step is to reprint
the modified list. Throughout this step there were some syntax errors, mostly
missing colons, which I figured out eventually.
Comments
Post a Comment