Today's lab will focus on random numbers, indefinite loops, and more on command-line scripts.

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

In-class Quiz

During lab, there is a quiz (for topics, see the quizzes page). It is closed notes and computers. No use of notes, books, computers, or other electronic devices. Put everything away except pens/pencils to take the quiz.

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 checking 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), use the graphical gEdit (you can find it via the search icon on the left hand menu bar).

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 (the Linux version of TextEdit) or IDLE.

When done, see the Programming Problem List.

What's Next?

If you finish the lab early, now is a great time to get a head start on the programming problems due early next week. There's instructors to help you, and you already have Python up and running. The Programming Problem List has problem descriptions, suggested reading, and due dates next to each problem.