| CSC 105 | The Digital Age | Spring 2009 |
Summary: In this laboratory, you begin the journey of learning to write computer programs in the Python programming language.
Contents
mkdir 105/code
cd 105/code
gedit &
print "Welcome to the wacky world of computer programming!"
code. (If it does not, you may need to use
the "Browse for other folders" option in order to save your file
in your 105/code directory.)
python first.pyIf all goes well, your program should print the greeting in the terminal window. If that does not happen, try to figure out why, or ask for help.
print "Are we having fun yet?"Save your program, and run it in the terminal window.
* *
* *
* *
*
* * *
* * *
* * *
*
* *
* *
* *
* *
* *
Remember to save your work before running your program in the terminal window.
Have fun with this exercise, but please work on your drawing for at most 5 minutes. We have a lot of fun things left to do!
In the previoius exercises, you used character strings, which are more commonly called just strings for short. A string is a single data element, composed of zero or more characters.
When typing a string directly into a program, as we have done, we enclose the string in quotation marks. This tells Python that the enclosed characters comprise a single string. (Note that the quotation marks were never printed in the output. They are not part of the string; rather, they are part of the program used to define the string.)
We can also store a string in a variable. To do so, we use an assignment statement such as the following. This is an instruction that tells Python to store the string "Sue" into a variable named name. We say that the value "Sue" is assigned to the variable name.
name = "Sue"Please note carefully, that (although it seems strange) the "equals sign" here does not denote equality. Rather, it is an instruction telling Python to assign the value on its right into the variable on its left.
name = "Sue" print "Hello", name, "how are you?"
Here is an alternative. We can concatenate strings (i.e., join them together to form a single new string) with the plus sign, as follows.
name = "Sue" greeting = "Hello, " + name + "! How are you?" print greetingHere, three strings are joined into one, and that one new string is assigned to the variable greeting. Then the value in greeting is printed. Try out this program to make sure you understand how it works.
strn = raw_input("Please enter a string: ")
print "You typed", strn
Try out the code above to see exactly what it does. When you run
the program, it should print a prompt asking you for data. It
should then wait until you type something and
press Enter. Only when you have done that will the program
move on to process the print statement.
Please enter a word: Hello ** Hello **
What is your name? Sue What is your favorite color? turqoise Hi, Sue! I like turquoise too!
color = "red" print color color = "blue" print colorGive this program a try to see if it does what you expect. You should see that the original value stored in color ("red") is overwritten by the new value "blue". A variable can hold one value at a time, so when you assign a new value to a variable, the old value is overwritten.
hue = "green" print hue color = hue print color print hue
color1 = "red" color2 = "blue"The purpose of your program is to swap the values in the variables color1 and color2, using only assignment statements. In other words, see if you can do it without having either of the string literals ("red" or "blue") appear anywhere else in your program. You may find it useful to use a third variable to solve this problem.
You can tell whether your program is successful by ending it with the following statement:
print color1, color2The output from your program should be: blue red.
For example, if the variable color holds the string "turquoise", then
Please enter a 6-letter word: sadist The first, third, and fifth letters are: s d s
color = "turquoise"
index = color.index("q")
print "The index number for the letter q within the word", color, "is", index
Try this program to convince yourself that it will, in fact, determine
the correct index number for the letter 'q'.
a. We can also isolate substrings within a string based on the index values of the characters we are interested in. Try out the following program to see if you can discover how the syntax for substrings works.
color = "turquoise" print "color = " + color print "color[0:1] = " + color[0:1] print "color[2:4] = " + color[2:4] print "color[1:5] = " + color[1:5] print "color[3:8] = " + color[3:8] print "color[:4] = " + color[:4] print "color[2:] = " + color[2:]
b. Write a program called time.py that prompts the user to enter the current time in HH:MM format, and then prints a message that states the time in a sentence. (To begin with, assume that the user will always enter two characters for the hour, even if one would do. Similarly, your program may print two characters, even if one would do.) For example, a sample run of your program might look something like this:
What is the time please (HH:MM)? 06:55 Thanks! It is now 55 minutes after 06 o'clock.
Now modify your program so that it will still work correctly if the user enters one-digit hours using only one digit. For example:
What is the time please (HH:MM)? 2:55 Thanks! It is now 55 minutes after 2 o'clock.Hint: You may find it handy to use three variables for this problem. First, find and store the index of the colon character. Then use that index to determine and store the hour and the minute values.
c.
CHALLENGE PROBLEM:
You may be familiar with Shirley's Ellis's Name Game. Ms. Ellis describes her procedure for developing phrases based on her colleague's names as follows:
Come on everybody!
I say now let's play a game
I betcha I can make a rhyme out of anybody's name
The first letter of the name, I treat it like it wasn't there
But a B or an F or an M will appear
And then I say bo add a B then I say the name and Bonana fanna and a fo
And then I say the name again with an F very plain
and a fee fy and a mo
And then I say the name again with an M this time
and there isn't any name that I can't rhyme.
She also gives us a number of examples:
Shirley!
Shirley, Shirley bo Birley Bonana fanna fo Firley
Fee fy mo Mirley, Shirley!
Lincoln!
Lincoln, Lincoln bo Bincoln Bonana fanna fo Fincoln
Fee fy mo Mincoln, Lincoln!
Write a program called NameGame.py that gets a name from the user and prints out a verse of the form Ms. Ellis suggests. Your program need not handle names that begin with vowels or with more than one consonant.