CSC 111: Intro to Computer Science through Programming

Lab 1

Due: Friday, Feb. 3 at 11:59pm on Moodle

The goal of this lab is to set up your software development environment, practice the programming techniques we have been learning in class, and begin using pair programming.

For this lab, first find your randomly assigned partner. Introduce yourselves - the person with the first name that comes first alphabetically should begin as the "driver", with the other partner as the "navigator". The driver will have the code open, and the navigator will have these instructions open.

After one hour, switch roles (and switch computers). At the end of the lab, email the code and transcript to the person who started as the navigator. Both partners should submit the same code on Moodle.

Credit: based on materials from Joe O'Rourke, Dominique Thiebaut, and Zelle Chapter 1 exercises.

Part A: Set up Python

  1. If you have not done so already, download and install the latest version of Python (or any version of Python 3) from the official Python website.

  2. To find IDLE, go to "All Programs" in Windows and "Applications" in Mac. Then find Python 3.x and from there, open IDLE. This will open the Python interpreter or the shell, where you can experiment and test your code.

  3. Go to "File" and "New File" to open the editor where you will write your code. I would recommend increasing the font size so your partner (and you!) and see the code better. Go to "IDLE" and "Preferences" (also sometimes under "Options") to increase the font size.

    IDLE

Part B: Experiment with Chaos

In this part we will make a few modifications to the chaos program we've discussed in class. Type in the chaos program from our textbook:

chaosCode

The following exercises are adapted from Zelle, Chapter 1.

  1. Save your file as chaos.py, in a folder that makes sense for this class (I would recommend creating a "CSC111" folder, and inside that, folders for "labs", "homeworks", etc). Make sure your program runs as expected by going to "Run" and then "Run Module". Then test out a few different values between 0 and 1 and observe the resulting behavior.

  2. Modify your program so that it prints out 20 values instead of 10. Test your program to make sure this works.

  3. Modify your program so that it asks the user how many values to print. Call this variable n. Then change the value 20 to the variable n so that the loop is executed n times (hints for this part are in the textbook, Exercise 1.5.).

You do not need to turn these modifications in.

Part C: User Input

Now create a new file and save it as lab1C.py (you will submit this on Moodle). At the top of your file, write the assignment name and the name of you and your partner, like the example below:

header

  1. Using the chaos program as a template, create a program that will ask the user for a few pieces of information, then construct a sentence. Start by defining a function called main() that takes no parameters, similar to the chaos program.

  2. Using the built-in function input(...), ask the user their name, college, and a course they are taking. Then use the built-in function print(...) to display a sentence about the user. It might be helpful to look back on the greet(person) function.

  3. Test out your program using your name and your partner's name. Make sure to invoke main at the end of your program. You should see output similar to the examples below:

    infoProgram

Part D: Strings and Loops

For this part, create a new file and save it as lab1D.py (you will submit this on Moodle). Like Part C, write the assignment name and the partner names at the top of the file.

  1. Inside this file, try out the code below:

    words1

    What happens when you run this module? This is how we can "multiply" a string.

  2. What about this code? This is another way to print strings together.

    words2

  3. Create two more lines that call print to produce the output shown below. You may change the variables word1 and word2.

    words3

  4. Without changing the variables from the previous question, produce the output shown below.

    words4

  5. Now we will use the concepts above to create a "pretty print" function for a name. In your file (below the "hello there" code), create a function called pretty_print(name). To start, just print the name, and invoke the function below (change to your name):

    print1

    (You may comment out the print calls for "hello there" if you like.)

  6. Now modify the pretty print function as shown below, and test out the result.

    print2

  7. Right now, the number of dashes does not match the length of the name. To make it match, define a variable for the length of the name as the first line of your function, as shown below:

    print3

    It may not seem like this line is doing much, but after we've defined this new variable, we can reuse it later on. Using the ideas from the "hello there" section, "multiply" the string "-" by the length of the name, to create output that looks like this:

    print4

  8. Finally, we will use a for loop to call pretty print for a list of names. Below and outside (no indent) your pretty print function, use a loop as shown below:

    print5

  9. Change the names to your names, plus a few other names of your choice. Test out your method to make sure it works like the output below:

    print6

  10. For fun if you finish early: modify your pretty print function to produce the output below.

    print7

    Also feel free to change the symbols to something else, or add more fanciness to your pretty print function!

Part E: Transcript and Submit

Finally, create a transcript that demonstrates your code working. From the shell, highlight all the testing you have done and all the output (since the chaos program) and copy it into a text file (.txt extension). On Windows you can use the program Notepad (under Accessories) and on Mac you can use the program TextEdit (under Applications). Save this file as lab1_transcript.txt.

Make sure that both you and your partner have a copy of all the code written during this lab, and the transcript. Both partners should submit the same files:

  • lab1C.py

  • lab1D.py

  • lab1_transcript.txt
on Moodle. If you did not finish, you can either meet to finish or finish separately. All labs must be submitted by Friday night.

Extra question: the names in this lab belong to which group of students?