Montana Tech of The University of Montana
Computer Science Department

CSCI 135
Fundamentals of Computer Science I
Fall 2019



LAB ASSIGNMENT 1

The goal of this assignment is to introduce you to programming in Python. You will learn how to write, compile and run your own programs. You will become familiar with some basic features of the IDLE Editor and the Python Shell, and learn how to open and use a command window.


Part 1: Hello new world!
As is traditional, your first Python program is the Hello world program. In this part, you will develop a program HelloNewWorld.py that displays something different from "Hello world!". (What you display is up to you.) If you are working from home or on your own laptop, first set up Python as described on the resources page. Since this is your first time using the Idle editor and shell, we'll show you step-by-step what to do.

1) Start by launching Idle. On lab machines, go to the Start Menu, select All Programs, then scroll down the list until you find Python 3.7. Select this and underneath, select IDLE. This will bring up the Python Shell window, which will look similar to the image below.


In the shell window, you can type Python commands and see the result immediately. Try typing in: print("Hello World!") and then press the Enter key. Remember, this is a programming language, so you have to be very precise in what you type - Python will not understand typos. You can also try typing in some math equations and use the shell as a calculator. Try typing in 3+4 and 3+4*3 and (3+4)*3, pressing the Enter key after each. We'll talk about why the results of those two last equations are different in a later lecture.

The >>> symbol in the shell where you type your commands is called the prompt. It's just a symbol that lets you know you can type in instructions at that point.

2) Select File -> New File from the top menu in the shell. This brings up an editor window where you can write your own programs and save them. Your program will be a list of Python statements that you will save and then run, or execute. Here you can type in the statements that make your program run as you want it to.


3) You can now start adding code to your file. In the screenshot below, note how I've added the required header comments at the top. We require headers on every source file you submit in the course so you want to get in the habit of doing this. You will be docked points if you don't. You'd be surprised how quickly in real-world programming you forget what a program you wrote does. Providing a good descriptive header is a good habit. It also helps us keep track of things during grading.


4) . Before you can actually compile and run your program, you will have to save it. Select File -> Save from the top menu. You will need to tell Python where to save your work. We strongly recommend a flash drive or cloud drive because there is no guarantee that you will get the same computer next time you're in lab, and you may not be able to retrieve your work.

Save this particular program under the name HelloNewWorld.py. We will tell you what you should name your program files, and as programs get more complex, this will become more important. Pay attention to how the name is capitalized.

Once you've saved your file, you can compile and run it. Select Run -> Run Module from the top menu, or alternatively, press the F5 key (top row of the keyboard). Your program will be compiled and will then run in the Python shell window.

 


Part 2: Greetings
Create a program Greetings.py that takes four command line arguments. The first command line argument is a general greeting word such as "Hello" or "Greetings". The second, third, and fourth command line arguments are the names of three people. The program outputs three lines of text. Each line of text has the greeting word, followed by a space, followed by the name of one of the three people, followed by an exclamation mark. Here is an example output given the input "Hello Keith Justin Joy":
Hello Keith!
Hello Justin!
Hello Joy!
A key difference of this program versus HelloNewWorld is the output of Greetings depends on the input. So running the exact same program but with the different input "Hola Alice Bob Carol" results in the output:
Hola Alice!
Hola Bob!
Hola Carol!
What in the world is a command line argument? We talked about this indirectly in class. You need to get input into your program in some way, and this is our first pass at just that. The command line is associated with a command window that looks something like the picture below. When running your program from the command line, you would type in python Greetings.py Hello Keith Justin Joy at the prompt. That is why these are called command line arguments.



OK, then how do I get to the command window? At the command prompt at the bottom of Windows 10, type in command and press enter. A new command window will open. You want to be in the directory/folder that holds your Python code. If that is on another drive (for example, the D: drive that holds all user files), you would type the drive letter followed by a colon: D: and press enter. Now you need to get to the directory that contains your files. Use the command cd pathname to get to the corrrect directory. For example, I would type D: and press enter, and then type cd users/mvandyne and press enter if my Python files were saved in that folder.

How do I access the command line arguments inside the Python program I'm writing? Check out LoveHate.py, and LoveHate2.py for basic examples of using command line input inside the program. (To open one of these programs, when you click on it, a window will popup asking what should be done with the file. Choose "Open with..." and then from the drop down menu choose a text editor. I generally use Windows Wordpad.) There are two things to notice here. First of all is the line import sys . This needs to be in the program so Python understands how to access the input arguments. The second thing is that in the two different examples there are two different ways shown of printing output, but the output of the two are slightly different because of spacing. You'll want to be careful in what you choose to use so that your output looks like ours.

When I run my program I get a long error message with the last line being IndexError: list index out of range, what is going on? Make sure you are running your program from the command line and giving it enough arguments (4 for this program, a greeting and three names).

My program's output isn't quite the same as yours for the example input above. Is that okay? Not really. Learning to program involves a lot of attention to detail. You should strive to exactly match our example outputs. There will be plenty of opportunity for creativity in future assignments and extra-credit levels. The assignment page is the "contract", sticking to the exact program specification will make grading easier for the TA, and that is good for all involved!

I accidently named my program Greeting or greetings or ... Is that okay? No. This will break our grading script. This is a surefire way to make your TA grumpy. Plus it is really easy to fix. Simply go to the top menu in the Idle editor and select File -> Save As, and give it the correct name. Then make sure that when you upload it to Moodle, you upload the correctly named file.

What should my program do if the user provides less than four command line arguments? Your program can just crash with an error. You do NOT need to provide error handling in this assignment. You can assume the user will provide exactly four command line arguments.


Part 3: Stairway
Create a program Stairway.py that calculates the total number of steps in a building's stairway as well as the total vertical height of the stairway. The program takes three command line arguments as input: So for example, for the input "10 13 0.75" your program would display:
Total steps: 130
Total height in feet: 97.5
For the input "5 21 0.81" your program would display:
Total steps: 105
Total height in feet: 85.05000000000001

How do I read an integer or floating-point command line arguments? The command line input, sys.argv[1], ..., sys.argv[3], comes into your program as text. You cannot perform mathematical calculations using text, so you must first convert the first two command line arguments to int variables, and the third argument into a float variable. We will talk about these more in our next lecture. For an example of how this is done, see ArgsExample.

What is with the crazy formatting of the total vertical height in the output for certain input values? This results from the way computers represents an infinite precision floating-point value in a finite amount of memory. We'll eventually learn how to format floating-point numbers nicely, but for now we'll just live with it.

Why can't I just represent the number of flights or steps per flight as a float instead of an int? Technically the program would still work, but in your programs it is best for a variable to use a data type that best reflects what it can hold. Our program isn't intended to support buildings under construction with partial flights of stairs, or flights that contain partial steps. If we used float variables, someone reading our program might think that such things were supported. Additionally, as we will see, int variables allow us to exactly check things such as if the building has 10 flights of stairs. Such checks are trickier with float variables


Grading Each of the three programs is worth 10 points each, for a total of 30 points. For each program, you will be graded according to the following criteria:

Grade ItemHelloNewWorldGreetingsStairwayPoints Earned
Program Compiles and Runs
2
2
2
Header Comment
2
2
2
Program Runs Correctly
4
4
4
Correctly Formatted Output
2
2
2


Submission. Submit all three programs HelloNewWorld.py, Greetings.py, and Stairway.py via the Moodle dropbox for Lab 1. Please don't "zip" them - just submit them as three files. Be sure each submitted source file has the required header with your name and a description of the program.

Page last updated: September 04, 2019