Computer Science 110
Foundations of Computer Science

Denison

NextClass Assignment 1

This assignment is expected to be completed in Wing and consists of basic exercises to get our feet wet with Python. It is more about the actual doing of the exercise than it is about "turning something in", although in the process, you will create a number of simple Python script files and will turn them in as a result of completing this assignment. In learning to program, you will need to
  1. Go through each exercise.
  2. Type in each one exactly.
  3. Make it run.
You must type each of these exercises in manually. If you copy and paste, you might as well just not even do them. The point is to gain programmer-centric skills of paying attention to detail, ferreting out syntax and logical mistakes, and training ourselves to see small textual differences (in this case, between what is given to you and what you type in). You are training your hands, your brain, and your mind in how to read, write, and see code. All of these get lost if you copy and paste. For this assignment, the plan is very simple:
  1. Write the exercise using the Wing101 IDE file editor.
  2. From within Wing, Run the exercise you wrote.
  3. Fix the exercise if it is broken.
  4. Repeat.
You are encouraged to install the necessary software and create and execute the exercises on your own computer.

Exercise 1

Suppose you want to write a Python program that displays the following three lines of text:
Nudge nudge
Wink wink
Know what I mean?

To write the program, you should use the Wing101 IDE to create a new text file, name it
nc1-1.py, and edit its contents to contain the following statements:
print 'Nudge nudge'
print 'Wink wink'
print 'Know what I mean?'

Using the green 'play' arrow, run the program and observe the output. If created and executed correctly, you should get exactly the desired lines of output.

Exercise 2

Next try a slightly more involved version, saving in a text file named nc1-2.py:
print "Hello World!"
print "Hello Again"
print "I like typing this."
print "This is fun."
print 'Yay! Printing.'
print "I'd much rather you 'not'."
print 'I "said" do not touch this.'

There are some variations here from the first exercise. Before you run the program, predict what the output to the console window will be. Reason about how the print statement works. If you get a syntax error, examine all of the error message and try to interpret what it is trying to tell you. Can you make sense of it? If you do not get a syntax error, introduce one by deleting the final " or ' (depending on the line). Then save the file are rerun it to see the syntax error message given by the Python interpreter. As above, try to make sense of the error message.

Before moving on to the next exercise, correct your nc1-2.py file so that there are no errors once again. The output should match the following:


>>> [evaluate nc1-2.py]
Hello World!
Hello Again
I like typing this.
This is fun.
Yay! Printing.
I'd much rather you 'not'.
I "said" do not touch this.
>>> 

Exercise 3

The next exercise lets you write your own print statements, and is based on Checkpoint 2.7-2.9 in the book. Create a text file named nc1-3.py in the Wing101 IDE with Python print statements that satisfy the following:
  1. Write a print statement that displays your name.
  2. Write a print statement that displays the following text:
    Python's the best!
    
  3. Write a print statement that displays the following text:
    The cat said "meow."
    
So when I execute my version of nc1-3.py, I get the following output:
>>> [evaluate nc1-3.py]
Tom Bressoud
Python's the best!
The cat said "meow."
>>>

Exercise 4

In this exercise, we will complicate things by making the exercise longer, thus increasing the chances for syntax errors, and we will also complicate things by having the print statements display more than one thing, and some of the work will involve how Python performs the evaluation of arithmetic expressions.

See if you can predict and/or reason about what the following program will do:


print "I will now count my chickens:"

print "Hens", 25 + 30 / 6
print "Roosters", 100 - 25 * 3 % 4

print "Now I will count the eggs:"
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6

print "Is it true that 3 + 2 < 5 - 7?"

print 3 + 2 < 5 - 7

print "What is 3 + 2?", 3 + 2
print "What is 5 - 7?", 5 - 7

print "Oh, that's why it's False."

print "How about some more."

print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2

The correct result when the above program executes should match the following. If it does not, then you should look for subtle differences in what you typed versus the code given on this page. Be prepared to explain why the result came out as it did. You may have to explore the meaning of some of the Python operators (like '%') in order to arrive at satisfactory explanations.
>>> [evaluate nc1-4.py]
I will now count my chickens:
Hens 30
Roosters 97
Now I will count the eggs:
7
Is it true that 3 + 2 < 5 - 7?
False
What is 3 + 2? 5
What is 5 - 7? -2
Oh, that's why it's False.
How about some more.
Is it greater? True
Is it greater or equal? True
Is it less or equal? False
>>>