Montana Tech of The University of Montana
Computer Science Department

CSCI 135
Fundamentals of Computer Science I
Fall 2021



LAB ASSIGNMENT 1

The goal of this assignment is to introduce you to arguments and variables in Python. You will become familiar with some basic features of the IDLE Editor and the Python Shell, and learn how to use command line arguments.


Part 0: Hello Arguments
Create a program HelloArg.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? You need to get input into your program in some way, and this is one way of doing just that. We saw some examples of how to use the Python input command in programs that were shown in class, but this is a faster way to get input to your program. Run your program from the Python shell. Select Run -> Run... Customized from the top menu. A window will pop up waiting for you to enter some data. This is where you would enter "Hello Keith Justin Joy" (without the quote marks).


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 might pop up 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. Or, depending on the browser you are using, you might just get a new window with the program code in it.) There are three things to notice in the program. 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 the command line arguments are called sys.argv[1], sys.argv[2], etc. You can use these as variable names. Or you can assign these to variables you create yourself. We will talk about lists on Friday, but this is basically a list of strings, with the greeting (Hello, Hola, or whatever you entered) in the first position, the first name in the arguments in the second position, and so on. The third 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 1: 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 talked about this in Monday's 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 Both programs are worth 10 points each, for a total of 20 points. For each program, you will be graded according to the following criteria:

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


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

Page last updated: August 31, 2021