Learning Objectives:

Software tools needed: web browser and Python programming environment with the pandas, numpy, and folium package installed.

Download the Skeletal Notes and Focus Questions to guide you while studying this lab.
These are a useful tool for note taking and you can keep these handy to study for and refer to during the final exam.

Quizzes

At the end of this lab, don't forget to take Lab Quiz 10! See the quiz page for details of the work due this week.

Using Python, Gradescope, and Blackboard

See Lab 1 for details on using Python, Gradescope, and Blackboard.

Random Range

Python has a built-in library for generating random numbers. To use it, you include at the top of your file:
import random
The random library includes a function that's similar to range, called randrange. As with range, you can specify the starting, stopping, and step values, and the function randrange chooses a number at random in that range. Some examples: Let's use that last example to have our turtle make a random walk:

Notice that our turtle turns a degrees, where a is chosen at random between 0 and 359 degrees. What if your turtle was in a city and had to stay on a grid of streets (and not ramble through buildings)? How can you change the randrange() to choose only from the numbers: 0,90,180,270 (submit your answer as Problem #10).

Indefinite Loops

We have been using for-loops to repeat tasks a fixed number of times (often called a definite loop). There is another type of loop that repeats while a condition holds (called a indefinite loop). The most common is a while-loop.

while condition:
   command1
   command2
   ...
   commandN
While the condition is true, the block of commands nested under the while statement are repeated.

For example, let's have a turtles continue their random walk as long as their x and y values are within 50 of the starting point (to keep them from wandering off the screen):

Indefinite loops are useful for simulations (like our simple random walk above) and for validating input. For example, the following code fragment:

age = int(input('Please enter age: '))
while age < 0:
    print('Entered a negative number.')
    age = int(input('Please enter age: '))
print('You entered your age as:', age)
will ask the user for their age, and continue asking until the number they entered is non-negative (example in pythonTutor).

More on Command Line Scripts

In Lab 3, we introduced the shell, or command line, commands to create new directories (folders) and how to list the contents of those folders (and expanded on this with relative paths in Lab 4 and absolute paths in Lab 5). In Lab 6, we wrote a simple script that prints: Hello, World. We can write scripts that take the shell commands we have learned and store them in a file to be used later.

It's a bit archaic, but we can create the file with the vi editor. It dates back to the early days of the Unix operating system. It has the advantage that it's integrated into the Unix operating system and guaranteed to be there. It is worth trying at least once (so if you're in a bind and need to edit Unix files remotely, you can), but if you hate it (which is likely), after you've tried it this once you can go back to an editor with graphical interface.

Let's create a simple shell script with vi:

  1. Open a terminal window.
  2. Type in the terminal window: vi setupProject
  3. To add text to the file, begin by typing i (the letter i) which stands for "insert"
  4. Type the lines:
    #Your name here
    #October 2017
    #Shell script:  creates directories for project
    mkdir projectFiles
    cd projectFiles
    mkdir source
    mkdir data
    mkdir results
    
  5. Click on the ESC (escape key-- usually on the upper row of keys) which escapes from insert mode.
  6. Type: :wq which stands for "write my file" and "quit" (the ":" is necessary-- it tells vi that you want to use the menu commands).
  7. This brings us back to the terminal window. Type ls to see a listing of the directory, which should include the file, setupProject, you just created.
  8. Next, we'll change the permissions on the file, so that we can run it directly, by just typing its name:
    chmod +x setupProject
    
    (changes the "mode" to be executable).
  9. To run your shell script, you can now type its name at the terminal:
    ./setupProject
    
  10. Check to see that your script works, type ls to see the new directories your script you created.

Troubles with vi? It's not intuitive-- here's more on vi:

If you hate vi, just edit using gEdit in Linux, TextEdit in MacOs, notepad in Windows or IDLE.

When done, see the Programming Problem List.

What's Next?

You can start working on this week's programming assignments. The Programming Problem List has problem descriptions, suggested reading, and due dates next to each problem.
Keep in mind that the due dates are one week late for flexibility (if one week there is a setback and you can't submit your programs, you will have time catch up). Still, each week you should work on the programming assignments for that week, even if they are due a week later. If you are on a roll, you are welcome to work ahead!!!