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.
See the introductory lab for details on using Python, Gradescope, and Blackboard.
import randomThe 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:
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).
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 ... commandNWhile 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).
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:
#Your name here #October 2017 #Shell script: creates directories for project mkdir projectFiles cd projectFiles mkdir source mkdir data mkdir results
chmod +x setupProject(changes the "mode" to be executable).
Troubles with vi? It's not intuitive-- here's more on vi:
When done, see the Programming Problem List.
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.