To start this assignment, download this zip file.
Lab 4c — Program arguments
Preparation
1 minute
Download the zip file for this lab, located above. This zip file has code that
you will use for this assignment. Extract the files and put them in your cs110
directory in a folder called lab4c
.
4 minutes
If you have a Windows computer use the guide for the Windows terminal setup to configure your system.
Intro
5 minutes
Review how a graphical file system works. Show:
- How to find the current working directory
- How to find the full path to the cwd
- Why “listing” the files in a folder is important
- How to change directories
- How to move up in the file system
Terminal practice
15 minutes
The terminal-practice
directory inside of the zip file contains a set of
programs. These are 100% complete and you do not need to write any code for this
exercise.
Your job is to find and run the three programs in this folder using only the terminal.
You need to do the following. We recommend doing this with the TA, with everyone taking turns to tell the TA what commands you need to type.
- Open the terminal.
- Use
cd
andls
to navigate to theterminal-practice
directory. - Find and run the
super.py
program. To do this you need to:- Use
cd
andls
to navigate to the folder where this is stored. - Run the program using
python super.py
. This will print a message telling you what arguments you need to run the program. - Run the program again using
python super.py <arguments>
, including any arguments you need.
- Use
- Find and run the
pi_day.py
program. To do this you need to:- Use
cd
andls
to navigate to the folder where this is stored. - Run the program using
python pi_day.py
. This will print a message telling you what arguments you need to run the program. - Run the program again using
python pi_day.py <arguments>
, including any arguments you need.
- Use
- Find and run the
lazy.py
program. To do this you need to:- Use
cd
andls
to navigate to the folder where this is stored. - Run the program using
python lazy.py
. This will print a message telling you what arguments you need to run the program. - Run the program again using
python lazy.py <arguments>
, including any arguments you need.
- Use
A small terminal cheat sheet:
cd lab4c
— changes into thelab4c
directorycd ..
— goes “up” to the parent directoryls
— lists files in the current directorypwd
— prints the path for the current directorypython program.py
— runs theprogram.py
Python program
Look for
10 minutes
This is another exercise to practice program arguments. Write a program that “looks for” a substring in phrases a person enters. You should run the program with:
python look_for.py <substring>
Where substring
is a string. When you run the program it lets you enter
phrases and then prints “YES” if the substring is found and “NO” if it is not.
You stop by entering a blank line.
% python look_for.py 'ee'
Enter a word or phrase: I love trees
YES
Enter a word or phrase: Do you need to sneeze?
YES
Enter a word or phrase: You look fabulous!
NO
Enter a word or phrase: Goodbyeee
YES
Enter a word or phrase:
Use the file look_for.py
. In it, you will find steps numbered from (1) to (4)
to write the program. You can refer to the
guide on program arguments for help.
A small program argument cheat sheet:
# get argument 1
phrase = sys.argv[1]
# get argument 2 and convert to an integer
number = int(sys.argv[2])
Simple calculator
10 minutes with a partner, 5 minute review
This exercise will give your practice with program arguments. You will write a simple calculator program that you can run with:
python calc.py <operation> <number1> <number2>
The calculator should support two operations:
- add — add the two numbers
- sub — subtract the second number from the first number
The numbers are both integers.
For example:
% python calc.py add 5 8
13
% python calc.py sub 40 21
19
Use the file called calc.py
. In it, you will find step-by-step instructions
labeled (1) through (5) to write the program. You can refer to the
guide on program arguments for help.
Remember you will need to convert the numeric arguments from strings to integers.
Grading
To finish this lab and receive a grade, take the canvas quiz.
Solution
We are providing a solution so you can check your work. Please look at this after you complete the assignment. :-)