First Summer 2006


Lab Four: Straight line Python Programs.
In this lab we start writing Python programs.

The best approach to this lab is: taking the Python textbook along for a ride, in front of your computer.

In the beginning we should be using the IDLE (IDE) interface as much as possible.

The IDLE is especially good for evaluating expressions.

Some of the simplest expressions are numbers.

Numbers are of two different types: with and without a fractional part.

We start by looking at integers first.

So we evaluate simple expressions:

Pages 30-33 discusses the numeric operators we start with in this class.

Expressions can use parens. Compare:

Some operators might surprise us. Evaluate:

Now you recall how on Thursday we said Python, like Alice, offers a virtual world.

The world that Python offers is that of a names, for variables that could hold your values.

So instead of putting a chicken on the stage we assign a value to a variable.

In class on Thu we discussed assignment statements like these:

n = 5
n = n + 1
You understood that the equals sign is an assignment operator, so information travels right to left.

This is a way to increment a variable by one.

We then asked for ways in which we could swap the values of two variables.

One way was to use a third variable:

n = 3
m = 5
k = n
n = m 
m = k
The other one was more arcane, clever, and also number specific:

n = 3
m = 5
m = n + m
n = m - n 
m = m - n
Was this working for negative values too?

The time dimension was important here in proving that this is indeed functioning correctly.

Let's now discuss this expression:

2 + abs(-3)
It should be clear what abs() does, it receive an argument and it returns a value.

Another type that we need to study refers to strings.

Chapter 2 starts with strings, pages 18-28.

Expressions, involving strings:

Strings are more complex than numbers, and that's where the analogy with Alice is stronger.

Useful string methods are listed on page 41:

The function raw_input( ) must be discussed by anaolgy with abs() mentioned earlier.

It produces strings, which need to be converted if they are to be used as numbers.

Conversion functions are listed on page 46.

Notice that Homework Three has been posted, and sample outputs will soon be added.

Meanwhile here is the lab assignment for today:

Problem One Write a program that produces this pattern:

     4
    4
   4
  4   4
 44444444
      4
      4
Problem Two Do the same with a pattern made out of double quotes.
     "
    "
   "
  "   "
 """"""""
      "
      "
Problem Three What would it take to write a program that produces this patern:
          .8.           8 888888888o          ,o888888o.
         .888.          8 8888    `88.       8888     `88.
        :88888.         8 8888     `88    ,8 8888       `8.
       . `88888.        8 8888     ,88    88 8888
      .8. `88888.       8 8888.   ,88'    88 8888
     .8`8. `88888.      8 8888888888      88 8888
    .8' `8. `88888.     8 8888    `88.    88 8888
   .8'   `8. `88888.    8 8888      88    `8 8888       .8'
  .888888888. `88888.   8 8888    ,88'       8888     ,88'
 .8'       `8. `88888.  8 888888888P          `8888888P'
Problem Four Write a program that asks the user for the lengths of the sides of a rectangle.

Then calculate and print the area of the triangle using Heron's formula.

(How do you know the three sides form a triangle?).

Problem Five Write an Eggy-Peggy translator: given a string, any string, the translator converts it to a new string by placing egg in front of every vowel.

Problem Six Write a program that prompts the user for two integers and then prints

Here's how your program might work:

frilled.cs.indiana.edu%java Two
Please enter your first integer number, then press Enter.
3
Please enter your second integer number, then press Enter.
6
3 + 6 = 9
3 - 6 = -3
3 * 6 = 18
avg(3, 6) = 4.5
dist(3, 6) = 3
max(3, 6) = 6
min(3, 6) = 3
frilled.cs.indiana.edu%

You, of course, have to write a Python program.

Problem Seven (Giving change) Implement a program that directs a cashier how to give change.

The program has two inputs:

Compute the difference, and compute the

that the customer should receive in return.

Here's how your program might work:

frilled.cs.indiana.edu%java Six
Type the amount due then press enter.
3.72
Type the amount received then press enter.
5
Give 1.28 in change as follows: 
   5 quarters
   0 dimes
   0 nickels
   3 cents
frilled.cs.indiana.edu%java Six
Type the amount due then press enter.
0.08
Type the amount received then press enter.

0.5
Give 0.42 in change as follows: 
   1 quarters
   1 dimes
   1 nickels
   2 cents
frilled.cs.indiana.edu%
You, of course, have to write a Python program.

The lab assignment for today is to solve these seven problems by writing seven programs.

Submit the programs to OnCourse (perhaps as a zipped archive) at the end of the lab.

The only think not covered here is: print. But I think you can get the information from the text.


Last updated: May 16, 2006 by Adrian German for A201/A597