A201

Assignment 4

Introduction to Python with RUR-PLE

Individual work in this lab and assignment.

In Lab

For some time we will be using the RUR-PLE environment for Python programming (a.k.a. Rurple, rurple, etc.). RUR stands for "Roberge's Used Robot", whose name is Reeborg, and PLE for "Python Learning Environment", which is used to program Reeborg, and can also be used for non-robotic programming.

For programming Reeborg in this assignment, and probably another one or two, you need to use the Rurple. For non-Reeborg programming, at least in this assignment, it is suggested you stay with the Rurple environment. Rurple should work on personal Windows machines, and perhaps others, following directions below. However, if you prefer (perhaps because you can't use Rurple on your personal machine) for non-Reeborg programming you may you may use any programming environment you choose, as long as you use a plain-text editor (as explained in class). The IDLE (an Interactive DeveLopment Environment, named after Monty Python actor Eric Idle) that comes with the standard Python installation is a development environment that runs on almost any machine.

On STC machines, use Start > Programs > Departmentally Sponsored > CSCI > Rurple to start the RUR-PLE programming environment. Ignore the black python.exe window that appears along with the Rurple window, but don't close it unless you want to kill Rurple.

For instructions on installing and running Python and Rurple on a personal Windows machine see the Personal Machines page of the course web.

Rurple initially displays its Home page. Ignore the three links at top, unless you're curious. Read the rest of the page, especially the Rules.

Next click the Lessons link at bottom, and then the About the browser link. Just above the Lessons link it is suggested that you use a regular browser, instead of Rurple, to view the lessons (which you can do by visiting with a browser the local file lessons_toc.htm in the en subdirectory of the lessons directory of the Rurple installation directory). But there is little point in this and it may be awkward to do, especially on STC machines.

For this assignment, read lessons 1 through 11, and parts of 25 and 26, as indicated below. In this assignment you are asked to turn in very little new code, but it is quite important that you do the Your turn exercises as you encounter them (except in lesson 9), or you will not have the experience necessary for following assignments.

Lesson 3 asks to you save a file. Use the file dialog to select a directory you own, such as a directory on your desktop or flash drive. (As usual with file save dialogs, you can create a subdirectory in a directory you own by clicking the Create New Folder icon to the right of the up arrow at the top of the dialog.)

After Lesson 5, try clicking the "Remove/add robot from/to world" button: the icon with +/- and robot, third from right in toolbar. Click it a second time. This is the convenient way to reset the robot position, which is necessary after running most programs. (We don't need the right-most toolbar button, but if you click it accidentally, just cancel all four of the dialogs that pop up.)

Why French, you may ask? Rurple's author, André Roberge, is a French physicist, turned college president, whose hobby is Python programming!

In Lesson 9, read the challenges, but you don't need to do them. (You have already expreienced in Alice that programs with lots of repetition are tedious to write and bad style.)

Do as much of this reading and practice programming as you can in the remaining time of this lab, and continue on your own after the lab. There is no submission other than the Alice practical test in this lab.

Assignment

Due 3pm, February 7th

In the Harvest time section of Lesson 11 an approach to harvesting the beepers in the harvest1.wld world is proposed that moves along streets. Do your best to complete this program using the code given in the lesson. If you need help you can download the solution file en_harvest1a.rur (right-click this link), which is provided in the rur_programsar_solutions folder of the rurple directory.

Now modify this program so that it harvests by moving straight along avenues, and save the result as harvest1b.rur. This only requires a few (strategically placed!) changes to the lesson's program.

Next, read Lesson 25, skipping for now the last section, "Comparing numbers". Use Rurple's Python: Code and Learn tab to try out some of the math operations.

In Lesson 26, for now just read the "Breaking lines" and "Defining functions" sections.

You are discouraged from breaking lines in this course (and our syntax rules do not deal with this complication). You can introduce variables as needed so no line gets so long this is necessary, and that generally is better style. But if you accidentally forget to provide a closing (right) parenthesis for every opening (left) parenthesis on a line, Python will assume you want to continue a statement line, as discussed in the "Breaking lines" section, resulting in an error message on a line after the one on which you forgot the closing parenthesis. Reading this section will help you better understand such potentially confusing error messages.

Defining functions using Rurple's Python: Code and Learn tab, as illustrated in the "Defining functions" section, is only satisfactory for such very simple examples. Since you can only edit the bottom line, you cannot fix mistakes on a line you have already entered. The same goes for any other Python "shell", such as the IDLE "Python Shell" window.

Programs in almost all languages should be written using a plain-text editor, very preferably one that provides support for the language, such you did writing Reeborg programs in the "Robot: Code and Learn" tab of Rurple. For non-Reeborg programs, it is recommended that you use the Rurple Python: simple editor tab or, if you prefer, the IDLE editor.

Rurple's editor is hopefully self-explanatory, with the tool bar button tool-tips (which pop up when you hover the mouse over a button). The ? "Help" button is not helpful and the "Run program with argument list" button you won't need for a while. As in the "Robot: Code and Learn" tab, the button with larger green triangle (third from left) runs the program, this time with output displayed in a separate panel, which you can place either below or on the right.

Note a quirk in the output frame: if the cursor is placed in the frame at a point where text follows, further output will be inserted at that point, even though the cursor has been moved to another frame. Best to clear text from the frame before each new test using the "erasor" button on the right of the tool bar.

Here is a skeleton for a very simple program that defines and tests a function for converting euros to dollars:

dollars_per_euro = 1.467

def print_euros(dollar_value):
    #...

print_euros(3)

The ... comment indicates that you are to replace that line with one that completes the program. The variable dollars_per_euro is a good idea, since this value changes from day to day, so for the most accurate results one would want easily find this value in the program and update it. With the given value, the function call at the end should print 2.04498977505. Save the program in a file named a4.py.

Finally, add to your a4.py file the definition of a function named print_total_length that takes two measurements in feet and inches and prints their sum, in feet and inches. Since earh measurement has two parts, it takes four parameters. Name the parameters feet1, inches1, feet2, and inches2, in that order. Assume all the parameters are integers.

For example, to find the sum of the measurements 3 feet 5 inches and 4 feet 8 inches, you would use the call:

print_total_length(3, 5, 4, 8)

which should print the following line of characters:

8 feet and 1 inches

It would be nicer to print inch instead of inches when there is only one inch, but we'll learn how to program such "conditional" behavior in Python next week.

When measurements are given in feet and inches, we expect the number of inches to be less than 12. Thus in our example results such as 7 feet and 13 inches, or even 0 feet and 97 inches, are in a mathematical sense correct, but they are not in the format expected of this program.

After its definition definition, include a few calls to your print_total_length function to test it.

Hints:

  • compute the total number of inches in the two measurements and store it in a variable local to the function
  • use integer division and the remainder (modulus) operator to determine the number of feet and inches (in the correct format)
  • recall that print statements can have multiple expressions, and whose values are printed separated by spaces

Submit your harvest1b.rur and a4.py files via Assignment 4 in the Oncourse assignment tool.