The purpose of today's lab is to practice using variables and expressions. There are 3 components:

Read the instructions below carefully and complete each exercise as indicated. For today's exercises, if possible, use pair programming. Only one copy of your code will need to be submitted (with each of your names and login ids), but you will take turns typing. Make sure each of you understands each step before continuing. At the end of the lab you will send a copy of all exercises to your partner so you will each have a copy.

Interactive Shell and Numeric Expressions

In Lab01 we worked with Thonny to write our programs. We wrote files and then asked Thonny to execute the commands in the file when we pressed the “Run” button. It is also possible to use Thonny to run python in “Interactive Mode”. Instead of writing an entire file of commands, we can instead give commands one by one and look at their result each time.

Do this ...
We will start by using Thonny to gain access to the interactive mode of python. Do this as follows:
  1. Open a terminal window, type cd cs108, and then mkdir lab02. This is where we will save all of our work from today. Open Thonny. If you have any files from last week still open, now would be a good time to close them.
  2. If you do not already see a Shell pane in the bottom half of the Thonny window, then in the menu system, select ViewShell.

The console at the bottom of the screen is now an interactive version of Python. It begins by printing some information about the version of Python that you are running, and then prints >>> as a prompt. At this prompt, you can enter any valid Python command, hit enter, and the system will respond with the result and a new prompt.

Exercise 2.1
Give Python each of the following commands, and be sure that you can explain the results:

As you can see, Python understands mathematical expressions, including precedence and associativity. Are any results surprising? Why would this be? Try a few other expressions and see if Python behaves as you expect.

To save your work, createa New file in Thonny. Then, select everything in the Shell window and use Control-C (or
Edit → Copy). Paste the contents into the new file. Now, Save the file as expressions.txt .

In case you were wondering, as with most programming languages, Python does not need a fancy IDE such as Thonny to work. In fact, Python can be run directly in a terminal window.

Number Puzzle

There are many different number puzzles; this one is usually attributed to Albert Einstein. Apparently he would write a number on a piece of paper, give it to someone else, and then let that person (or anyone else) pick any 3 digit number where the first and last digits differed by at least 2. After some math, regardless of which number was chosen, the result was always the number written down by Einstein before any number was chosen. Today, we will write a program that simulates this puzzle.

Exercise 2.2
Open a New file and save it in your lab02 folder with the name einstein.py. Add header documentation for the purpose of the program, the date, and you and your partner's names and login ids. Now, implement the following algorithm:
  1. Prompt for a 3 digit number where the first and last digits differ by at least 2 and store the entered number in a variable named number. Note: For our purposes, assume that the user of the program can correctly choose a number.
  2. These next four steps compute and store the reverse of the number. For example, if the user enters 123, you should compute 321.
  3. Store the 100’s digit in a variable called digit1.
  4. Store the 10’s digit in a variable called digit2. Hint: You can get this digit using this expression: (number//10)%10.
  5. Store the 1’s digit in a variable called digit3.
  6. Store the integer equal to these three digits in reverse order into a variable called rev_number.
  7. As a challenge, you can try to do the preceding four steps all in one statement. It is possible to do it that way!
  8. Store the difference between number and rev_number in a variable called difference. We want this result to always be positive, so use the abs function. e.g. abs(4 - 8) is 4.
  9. Store the reverse of difference in a variable called rev_diff. Note that this requires you to do those four steps again, which this new number.
  10. Print the sum of difference and rev_diff

Your program should always print a result of 1089.

Scalable Stick Figure

In lab01 we used turtle graphics to draw pictures. One of the limitations of the approach we used there was that there was no easy way to scale our pictures to be bigger or smaller. In this exercise, we will use expressions and turtle graphics to create an easily scalable stick figure.

Do this...

Create a new file called stick_figure.py in your lab02 folder. Add appropriate documentation for your goal, the date, and the authors.

Add appropriate import statements to use turtle graphics and the math library, which is helpfully called math .

Often within a program there are values that are not intended to be changed. The convention (or “good idea”) within Python is to give such values a name, using all capital letters, to help future readers of the code understand that this value should remain constant throughout program execution. Variables with names that are all capital letters are then referred to as “constants”.

Exercise 2.3
Use turtle graphics to draw a stick figure as shown. Below are some tips:
  • Begin by creating a constant in your program called UNIT and have it refer to a value of 50.
  • In the picture, the distance between tick marks is a UNIT length.
  • When the code is complete, you should be able to change the value of UNIT and get a differently sized stick figure when you run your program. Be sure to check that this is the case before your submit.
  • To determine the length of each leg you can use the Pythagorean theorem.

Checking In

We will grade these exercises according to the following criteria:

Submit your solutions to these lab exercises using your appropriate submission environment.

If you worked with a partner, you can (and should) send a copy of the files using the instructions here: send copy instructions

If you’re working on a lab computer, don’t forget to log off of your machine when you are finished!