NextClass Assignment 1
- Go through each exercise.
- Type in each one exactly.
- Make it run.
- Write the exercise using the Wing101 IDE file editor.
- From within Wing, Run the exercise you wrote.
- Fix the exercise if it is broken.
- Repeat.
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:- Write a print statement that displays your name.
- Write a print statement that displays the following text:
Python's the best!
- Write a print statement that displays the following text:
The cat said "meow."
>>> [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 >>>