# CS21 Lab 2: Numbers and Strings

Due 11:59pm Tuesday, February 3

Run update21, if you haven't already, to create the cs21/labs/02 directory. handin21 looks for your lab 2 assignments in the cs21/labs/02 directory, so make sure you create your python programs in there:

```\$ cd cs21/labs/02
\$ xemacs line.py &
```

Computing distance and slope

Write a program, in a file named line.py, that asks the user to enter the value of two points in the Cartesian plane and then computes the slope of the line defined by those points and the distance between the points (formulas are on p.73, exercises 6 and 7).

Here is what two runs of your program might look like:

```\$  python line.py
Given two points, (x1, y1) and (x2, y2), this program computes
the distance between them and the slope of the line they define

Enter the x coordinate of the first point: 1
Enter the y coordinate of the first point: 1
Enter the x coordinate of the second point: 4
Enter the y coordinate of the second point: 5

The distance between (1,1) and (4,5) is 5.00
The slope of the line they define is 1.33

\$  python line.py
Given two points, (x1, y1) and (x2, y2), this program computes
the distance between them and the slope of the line they define

Enter the x coordinate of the first point: 6
Enter the y coordinate of the first point: 12
Enter the x coordinate of the second point: 10
Enter the y coordinate of the second point: 30

The distance between (6,12) and (10,30) is 18.44
The slope of the line they define is 4.50
```

Try using string formating to limit the number of places printed beyond the decimal point.

To use math library functions, remember to add this to the top of your program:

```from math import *
```

2. Finding average word length

In this problem you will calculate the average word length in a phrase entered by the user. Edit the file avgLength.py to create your solution. Your program should produce the following sort of interaction with the user:

```\$ python avgLength.py
This program calculates the average word length in a phrase.

Enter a phrase: help is on the way
Average word length: 2.8
```

In the phrase above, the word lengths are 4, 2, 2, 3, 3, for a total of 14 and an average of 14 divided by 5 which is 2.8.

Acronyms

Write a program in a file named acronyms.py, that prompts the user to enter a phrase, and then converts the phrase to it acronym. Here are a couple runs of what your program might look like:

```\$  python acronyms.py
This program creates acronyms from a phrase.
Enter a phrase: the World needs one more acronym
The acronym of "the World needs one more acronym" is: TWNOMA

\$  python acronyms.py
This program creates acronyms from a phrase.
Enter a phrase: to be or not to be
The acronym of "to be or not to be" is: TBONTB
```

Submit

Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt. You may run handin21 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize after handing in some programs that you'd like to make a few more changes to them.