Download Python Lab 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Science 101
Programming Lab 1
Goals:
In this lab you will learn to work with the JES environment for Python. You will
gain experience with Python expressions, operators, error messages, etc. You will
also write, save and turn in your first Python programs.
Working with JES:
JES (Jython Environment for Students) is the tool we will use for working with
Python. There are two primary modes for working with JES. These modes
correspond to the two main panes in the JES window.
First there is the interpreter (or shell) pane at the bottom. Our text refers to this as
the Command Area. In this area we are presented with a prompt (>>>) to enter a
Python statement or expression – issue a command. When we do so, the interpreter
immediately interprets and executes the statement or evaluates the expression,
showing the result. If we have syntax errors, these will be indicated. This mode is
great for exploring Python statements to see what they do and to see the effects of
small changes – just try it and see what happens!
The only drawback to the command area is that we cannot save the work we have
done. It is great for trying things out, but not for work that we want to save and
reuse. For work that we want to save, reuse, or turn in, we use the top pane or the
Program Area. There we write complete programs as Python functions or groups of
functions, using the editor provided. Then we can save the program file, load it into
the interpreter and execute it. More on this later.
Working with the JES Command Area:
Below is a brief session with the interpreter:
Working with the Program Area
A tradition with programmers is that the first program to be written when you learn
a new language should be a “Hello, World!” program. This program simply
displays these words. The spirit of the tradition is that you should write a very
simple program first, just to be sure the software is installed correctly and that you
know how to get started. So let’s do this in such a way that we can save the program
and call it up later. With JES, we create programs as Python functions.
So in the command area, we write a helloWorld function:
Next, we save the program and then click the Load Program button. At that point
we can give the command in the Command Area to have the function executed.
F
When you save programs, they should be saved on your H drive, not on the C drive of the
lab computer. If we come back to JES for another session and want to work with this
program some more, we would go to the file menu and Open the program file. Then we
could make changes, save, load and execute.
Your turn – working with Python Interpreter
Start JES and enter each of the following expressions or statements in the
Command Area in the order given and see what happens. The goal here is to get
familiar with working in the interpreter mode, and to begin to understand some of
the statements, operations, and other properties of Python.
24
3.1416
8/9
8.0/9.0
8.0/9
age
“age”
age = 40
age
age = input(“Enter your age: “)
age
12 x 60
12 * 60
12 * “60”
12 + “60”
32 / 20
32 % 20
type(age)
age = “40”
type(age)
type(8)
type(8.0)
print Hello
print “Hello”
print “Hello” + “Joe”
pi
abs(-12)
max(4,3)
randrange(1,7)
from random import *
randrange(1,7)
randrange(1,7)
(Enter your age at prompt)
Working with Media Files (Hint of things to come)
1. In the Program Area at the top, type the word pickAFile. Now click on the
Explain button at the bottom to see help on pickAFile.
2. In the Command Area at the bottom type the command
myFile = pickAFile()
Navigate to the Shared folder in our course folder in the L Drive. Choose the file
named Mystery.jpg.
3. Use the Program Area to learn about the functions makePicture, show and
explore.
4. Now type the commands
print myFile
myPicture = makePicture(myFile)
show(myPicture)
explore(myPicture)
5. Now repeat the command of Step 2 above, but this time choose the file
Mystery.wav from the Shared folder.
6. Now type the commands
print myFile
mySound = makeSound(myFile)
play(mySound)
# Use head set
explore(mySound)
7. Learn more about the Mystery tune at
http://www.generalssports.com/information/athletics_history/swing
Writing Python Programs
Setting up your folders: Outside of JES, go to H drive directory and create a new
folder called MyPython. Now inside that folder create another folder named Lab1.
This is the folder where you will save your programs from this first assignment.
For each of the programs, you should begin with comments at the top, giving your
name (and your partner’s name), the name of the course, the name of the program
and a brief description of the purpose of the program. Also, it is important that you
name your programs exactly as specified so that I will be able to find your work
easily.
For each program, you will make a function with the appropriate name:
Program 1 – Introduction
This program should be saved as Introduction.py. The function should be
named introduction. When executed, the program should display your name,
your home town, your projected graduation year from W&L, and your major
(or intended major) along with the same information of your partner. These
pieces of information should be printed on separate lines. Note that there is no
input, simply “print” statements. When you have the program working, print a
copy to turn in.
Program 2 – Tickets
This program should be saved as Tickets.py. The function should be named
tickets. Assume that tickets for a concert are $40 for adults and $25 for
children. Your program should allow the user to enter the number of adult
tickets needed and the number of child tickets needed. The program should
compute and display the total cost of the order.
Program 3 – CToF
This program should be saved as CToF.py with function named CToF. In
class we wrote a program to convert Fahrenheit temperatures to Centigrade.
This program will go in the opposite direction. You program should get a
Centigrade temperature from the user and then compute and display the
corresponding Fahrenheit temperature.
Turning in your work
When you have finished, you should turn in printed copies of the three programs.
Be sure you have the comments at the top as instructed.
Secondly, you are to turn in the programs electronically. Here the idea is that you
are going to paste a copy of your entire Project1 folder into your Returns folder. To
do this, out of JES
1.
2.
3.
4.
5.
6.
7.
Open your MyPython folder.
Select the Project1 folder and select Copy from the Edit menu.
Now in MyComputer go to “AcadShared on 'MFsAcad1' (L)”.
There you should see a folder for our course – open it.
Now open the Returns folder.
You should see a folder with your name.
Paste your Project1 folder into the folder with your name.