summaryrefslogtreecommitdiff
path: root/projects/2/README.md
blob: b2408348e27fca9a4bc4809972b49c6c92330db0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# Project 2: Introduction to Programming in Python

## Introduction

The purpose of this project component is to provide a first experience in the
actual writing of computer programs. Using a relatively simple introductory
scripting language (Python), you will create program code in an editor (vim),
save it to your disk, and run it from the command line prompt.

You will gain experience in planning, writing, debugging, documenting, and
running simple programs. Learning Python serves as a convenient stepping-stone
to more complex object-oriented languages, such as Java (lol).

In this project, you will use online resources, including downloadable Python
tools and tutorials.

Before writing programs, it is useful to plan the programming steps and actions
by writing an algorithm. An algorithm is a set of plain English language
commands or steps, each of which is then replaced by the appropriate command
line for the programming language used. This technique becomes less useful when
using complex object-oriented languages such as Java, but may be helpful in the
early stages of learning to design programs. You will write algorithms for your
first two programming exercises in this project.

The project will be graded for completeness and correct functioning of programs.

## Acquiring the Tools

Download and install Python version 2.2 or higher by following the instructions
at the Python download site.

The downloaded file will be called Python-2_2.exe. After downloading, run this
file to complete the installation. You can view the installed components from
your "Start" list.

## Writing Algorithms

In the traditional approach to programming, the program is seen as a series of
steps, which may include branches and loops. A branch occurs when a program may
go in two or more different directions, depending upon a logical condition or a
choice made by the user. A loop is a situation where a particular step, or
series of steps, may be repeated until a certain condition or choice occurs.

The following simple example of an algorithm includes both elements, and
describes a simple program for performing addition or multiplication.

1. display the program name `"Addition and Multiplication"`
2. display the options menu `"(A)dd, (M)ultiply"`
3. request and store input of user choice A or M as `choice`
4. if `choice` does not equal "A" or "M", go to Step 9
5. request and store the first number to be used as variable `X`
6. request and store the second number to be used as variable `Y`
7. if `"choice" = "A"` go to Step 11
8. if `"choice" = "M"` go to Step 13
9. display message `"Choose A or M"`
10. go to Step 3
11. display `"Sum is" X+Y`
12. go to Step 3
13. display `"Product is" X*Y`
14. go to Step 3

## Writing, Storing, and Running Python Programs

The actions involved in creating and running Python programs are relatively
simple:

Input the code using vim, and save the file as `*.py` (e.g., prog1.py).
Save your programs in the Python folder on your hard disk.

1. Open the MS-DOS prompt window.
1. Change directory from Windows to Python22 (enter "cd\", followed by "cd python22").
1. Run the Python interpreter on your program by typing python filename.py, where "filename" is the actual name of your saved `*.py` file (e.g., python prog1.py).

## Learning Python

Go to A Beginner’s Python Tutorial, and work through the first seven lessons.

Please note that the tutorial describes running programs for an earlier version of Python, and follows:

> "Edit" menu-> "Run Script"

In later versions of Python, programs are run as follows:

> "Run" menu-> "Run Module" (or simply hit F5)

Note: If you need more help, the internet has several instructive sites, for example, www.learnpython.org/.

## Programs

The programs you will write for this project are copied with permission,
or adapted from, the exercises in A Beginner's Python Tutorial.

Each program should be written, tested, and debugged. The first two programs
should also be fully commented, with each line documented by a descriptive
comment. The remaining programs should have a single comment line at the
beginning to describe the function of the program. All programs should start
with a display of your name, student id#, and the program number and name.
When storing the files, name them as `prog1.py,` `prog2.py`, etc.

### EXAMPLE OF AUTHOR/PROGRAM INFORMATION OUTPUT

```bash
Program author: B. Rubble

ID#: 1234567

Program 1—Math Functions
```

### PROGRAM 1—MATH FUNCTIONS

Write an algorithm for a program that shows the use of all six math functions. Write, test, and debug the program using Python.

SAMPLE OUTPUT (not including author/program information)

```bash
ADDITION: 2+2=4

SUBTRACTION: 4-2=2

MULTIPLICATION: 4*2=8

DIVISION: 4/2=2

EXPONENT: 2**3=8

REMAINDER: 5%2=1
```

### PROGRAM 2—USING INPUT

Write an algorithm for a program that receives, as input from the user, 2 string
variables and 2 integer variables; then joins together and displays the combined
strings; and finally multiplies the two numbers on a new line. Write, test, and
debug the program using Python.

SAMPLE OUTPUT (not including author/program information)

```bash
Input string 1? Billy

Input String 2? Bob

Input integer A? 23

Input integer B? 2

BillyBob

46
```

### PROGRAM 3—LOOPS AND IF CONDITIONS

Write a program that requests a password after the author/program information
is displayed. Make the password "hello". The program should then ask the user
for their name: if the name entered is the same as your name, the program should
respond with "What a great name!"; if they enter "Madonna" or "Cher", the
program should respond "May I have your autograph, please?". For any other
input, the program should respond with "(input name), that’s a nice name".

```bash
SAMPLE OUTPUT (including author/program information)

Program author: Barney Rubble

ID#: 1234567

Program 3—LOOPS AND IF CONDITIONS

Password? unicorn

Password? opus

Password? hello

Welcome to the second half of the program!

What is your name? Barney

What a great name!

 
```

ALTERNATE OUTPUTS

```bash
What is your name? Cher

May I have your autograph, please?

What is your name? Bill

Bill, that’s a nice name.
```

### PROGRAM 4—FUNCTIONS

Rewrite the `area.py` program (shown below, or in the Creating Functions section
of the tutorial) so that it has separate functions for the perimeter and area of
a square, a rectangle, and a circle `(3.14 * radius**2)`. This program should
include a menu interface that has `‘exit the program’` as one of its choices.

#### SAMPLE PROGRAM EXECUTION

```python
# Area.py

#This program calculates the perimeter and area of a rectangle

print "Calculate information about a rectangle"

length = input("Length:")

width = input("Width:")

print "Area",length*width

print "Perimeter",2*length+2*width

 
```

#### SAMPLE OUTPUT (not including author/program information)

```bash
CALCULATIONS MENU

1) AREA (SQUARE)

2) AREA (RECTANGLE)

3) AREA (CIRCLE)

4) PERIMETER (SQUARE)

5) PERIMETER (RECTANGLE)

6) PERIMETER (CIRCLE)

7) EXIT

INPUT MENU CHOICE (1,2,3,4,5,6 OR 7)? 2

YOU HAVE CHOSEN AREA (RECTANGLE)

INPUT WIDTH? 8

INPUT LENGTH? 4

AREA IS 32

INPUT MENU CHOICE?
```

#### Packaging and Submitting

When submitting your project, use WinZip or a compatible program to compress all
the required files into a single archive.
There should be four files in total:

1. `prog1.py`
2. `prog2.py`
3. `prog3.py`
4. `prog4.py`

## Grading

All programs must be fully debugged, and must run without errors.
No marks will be given for programs that do not run.

Marks are assigned as follows:

| Program | Percentage                              |
| ------- | ----------                              |
| 1       | 10% (7% for program, 3% for algorithm)  |
| 2       | 20% (15% for program, 5% for algorithm) |
| 3       | 30%                                     |
| 4       | 40%                                     |