Module 1: Introducing Python

The first module of GIS Programing introduced the basic of Python. There seems to be a pretty steep learning curve. Once I learn the basics programming will probably be easier. In this module's lab, I created code that prints my last name, fixed provided code, randomly populated a number list, and removed an unlucky number.

The image above is the output from my code. I had to print my last name by splitting a string. Then, fix two code errors the run the dice rolls. Finally, write code to make a random list of 20 numbers and remove an unlucky number from the list. Below is a flow chart describing my code.


In step 4, I created a while loop and used the remove() method to remove the number 2 from the list. I initially created a while loop telling it to remove the unlucky number until the count > 0. This did not work. I was getting the error “list.remove(x): x not in list.” I went to Google and the textbook to solve this issue. I found this resource https://www.geeksforgeeks.org/python-list-remove/  . It helped me switch from using the count variable I made in step 3 to the count method. I then switched it to while the luckyList still has a count for my unlucky variable remove it. Then print the new list. This ran correctly and printed a new list without the unlucky number.

I really struggled with step 3. I got code that would output the correct list but did not use an if statement or break. This is what I originally had:
    import random
    luckyList=[ ]
    while len(luckyList) <= 19:
        x=random.randint(0, 10)
        luckyList.append(x)
        x += 1
    print (luckyList)

I had to do a lot of research to determine what I was doing wrong in the while loop. I switched the len() from the while line to the if line. Further, I eventually figured out the len() function cannot contain an equals sign. The arithmetic operators need to be after the (). Also, one equals sign is not enough. The equals condition for if statements is ==.

Here are some resources for future reference that I used to help trouble shoot step 3:



Comments