Skip to content

Week 1: print() and input()

Week 1 is mostly about tools, so there is very little Python to learn yet. The two functions you have seen, print() and input(), are enough for everything on this page. The point of these exercises is not the Python; it is to run the write–save–run loop in Thonny enough times that it stops taking any thought, so that when real content starts next week the tools are not the thing slowing you down.

Save each program in your ~/CS149 folder, next to the hello.py you wrote in the first lab. The exercises are ordered from easiest to hardest. Each one has a submission on Gradescope, so you can check your own work as you go. The weekly reflection asks you to include a program or two, so keep what you write.

Ex1.1 Casting Spells

Write a program named sorcery.py that prints the following quotation from Structure and Interpretation of Computer Programs, by Harold Abelson and Gerald Sussman.

We are about to study the idea of a computational process.
Computational processes are abstract beings that inhabit computers.
As they evolve, processes manipulate other abstract things called data.
The evolution of a process is directed by a pattern of rules called a program.
People create programs to direct processes.
In effect, we conjure the spirits of the computer with our spells.

Produce each line with a separate print statement, and match the text exactly, character for character.

There is nothing clever here on purpose. This is a typing-accuracy exercise, and the thing to practice is the loop: write a line, save the file with Ctrl+S, run it, look at the output, fix what is wrong, run it again.

Ex1.2 Nice to Meet You

Write a program named greeting.py that asks the user for a name and a hometown, and then greets them. Use exactly the prompts "What is your name? " and "Where are you from? ", including the space after each question mark.

When your program runs, the shell should look like this. The values after the prompts are what the user typed; everything else your program printed.

What is your name? Madison
Where are you from? Roanoke

Nice to meet you, Madison
I hear Roanoke is a great place to be from

Notice the blank line between the last prompt and the first line of output; a print() with no arguments at all prints one. Notice also that your program displays nothing until both questions have been answered.

The space at the end of a prompt

Write your prompt as input("What is your name? "), with a space before the closing quotation mark. Without it, the user's typing runs straight into your question mark, which is why nearly every prompt in this course ends with a space.

Ex1.3 Mad Libs

A Mad Lib is a story with words missing, filled in by someone who has not read the story.

Write a program named madlib.py that asks for three words and then tells a short story using them. Use exactly the prompts "Enter an adjective: ", "Enter a plural noun: ", and "Enter a verb: ".

Enter an adjective: soggy
Enter a plural noun: raccoons
Enter a verb: juggle

The dining hall served something soggy again today
Outside the library a crowd of raccoons had gathered
Nobody was surprised when they started to juggle

Run it several times with different words and check that all three lines still read correctly. Your program has to work for whatever the user types, not just for the three words above.

Numbers are fine too

Try running your program and typing 149 as the adjective. It works, and your program prints 149 without complaint, because as far as your program is concerned it received three pieces of text and printed three pieces of text. That is worth noticing now, because next week you will find out what happens when a program tries to do arithmetic with what the user typed.

Ex1.4 The Interview

The three programs so far asked all their questions first and printed everything afterward. They do not have to be arranged that way: print() and input() run in the order you write them, so a program can react to one answer before asking the next question.

Write a program named interview.py that interviews the user. Use exactly the prompts "What is your name? ", "What is your major? ", and "Where are you from? ".

What is your name? Madison
Hi Madison
What is your major? computer science
Where are you from? Roanoke

Madison is from Roanoke
Madison is studying computer science
Good luck this semester Madison

Three things about this one are different from the earlier exercises, and each is the actual point of it:

  • The greeting Hi Madison is printed between the first question and the second, so your print and input statements have to be interleaved rather than grouped.
  • The name is printed four times in all, from one input(). That is what a variable is for: you read the answer once, store it, and use it as often as you like.
  • The summary reports the hometown before the major, even though the program asked for the major first. The order in which you ask questions and the order in which you use the answers are independent.

Ex1.5 Dining Hall Menu

Text lines up in columns when it is separated by tab characters. You cannot type a tab inside a string in Thonny's editor, because pressing Tab indents your code instead, so Python gives you a way to write one: \t inside a string means one tab character.

Write a program named menu.py that prints the following menu, with the prices lined up in a column.

Item            Price
----            -----
bagel           1.50
coffee          2.25
smoothie        4.75
quesadilla      6.00

The gap between the two columns must be made of tab characters, not spaces. The block above is drawn with spaces, because a web page cannot show you a tab, so read the requirement rather than counting what you see.

How many tabs each line needs is not the same on every line, and that is the interesting part of this exercise. A tab does not move a fixed distance; it moves to the next column that is a multiple of eight. bagel is five characters long, so one tab only reaches column 8 and a second tab is needed to reach column 16, where every price begins. smoothie is eight characters long, so a single tab already reaches column 16, and a second one would push its price out to column 24. Work out the rest of the rows for yourself.

Run your program and check that the prices form a straight column in the Shell. If one price is out of line, count the characters in the item name on that row.

Tabs are not spaces

You could line these columns up with spaces instead, and it would look identical. Tabs are worth learning anyway, because a tab is a single character that means "move to the next column," which is how programs write data that other programs read. You will see tab-separated data again when this course reads files.

Ex1.6 Kilroy Was Here

During World War II, "Kilroy" was popular graffiti among American soldiers. Write a program named kilroy.py that draws him:

             """"
            |.  .|
    ----///---()---\\\----

        Kilroy was here

This is the hardest exercise on the page, because the whitespace and the punctuation are both part of the answer. The indentation above begins with a tab character, which this page cannot show you directly, so build the picture from the description below rather than by counting what you see. The picture is five lines tall:

  • The first line begins with a tab character, then nine spaces.
  • The second line begins with a tab, then eight spaces.
  • The third line begins with a tab, and the dashes start immediately after it.
  • The fourth line is completely blank, with no whitespace on it at all.
  • The fifth line begins with a tab, then four spaces.
  • No line has any trailing spaces.

Quotation marks and backslashes need care

A string written with double quotation marks cannot simply contain a double quotation mark, since Python would read it as the end of the string. Write that line with single quotation marks around it instead, or escape each one as \".

A backslash cannot appear on its own either, because a backslash is how an escape like \t begins. Write each backslash you want as a pair, \\, which means the three backslashes in the third line take six characters in your code. If you write a single backslash in front of some other character, Python warns you about an invalid escape sequence rather than doing what you intended.

Getting this to match exactly is fiddly, and that is the experience worth having. When your output looks right but you are not certain it is right, the surest check is to run it and compare the result against the picture above one line at a time.

An extra challenge. Once your version works, try producing the whole picture with one print statement. Just as \t means a tab, \n means a newline, so a single string can hold all five lines. The output should be identical to what your first version printed.

When you're done

Commit and push whatever you wrote, the same way you did in the lab:

git add -A
git commit -m "Add week 1 practice programs"
git push

Then go to your other machine, run git pull, and open one of these programs there. Getting a program you wrote on one machine to run on the other is the actual skill of Week 1, and this is a good chance to prove to yourself that your setup works.