| CSC 105 | The Digital Age | Spring 2009 |
Summary: In this laboratory, you continue the journey of learning to write computer programs in the Python programming language.
Contents
Open a terminal window, and move to the code directory you created in the first Python lab:
cd 105/code
Next, open the text editor gedit as shown below, and then arrange your windows so that you can see the terminal window and gedit at the same time.
gedit &
Remember that you will move back and forth between these two windows as you did last time:
Next, please quickly review the previous lab so that the lessons you learned there will be fresh in your mind for you to use today.
a. In the last lab, you worked with strings and found that the plus sign can be used to concatenate two strings together. For a quick review, write a program that asks the user to enter two words, and then prints the words concatenated together.
An example run of your program might look like this:
Enter a word: rain Enter a second word: bow The compound word is rainbow.
Now see if you can modify your output to look like the following. This one is a bit trickier.
Enter a word: rain Enter a second word: bow rain + bow = rainbow.
b. Try entering numbers into your program when it asks you to enter words. Does the result make sense for numbers?
Not really. Most of us would prefer the plus sign to add two numbers rather than appending them together. Luckily, Python can do this too.
The issue here is that, although you just entered a number, Python recognized it as a string that happened to contain numeric digits. (Remember that we store integers and characters very differently, even if the characters happen to be numeric digits.) What we need is a way to let Python know that the data it has received are numbers. There are two ways to do this.
First, we can convert a character string (that happens to contain numeric digits) into an integer with the function int, as in the following example. Here str is a variable that contains a string, int is the conversion function, and the result (a number) gets assigned into the variable num.
num = int(str)
Please modify your previous program to incorporate the function int. (After accepting each of your input data values from the user, convert them to numbers, and then add them. You may also want to change your printed prompts, so that they ask the user to enter numbers not words.)
c. A second option is as follows. To accept a number directly from the user (and have Python recognize it as a number immediately), we use the function input rather than the function raw_input, as shown in the example below.
number = input("Please enter a number: ")
Now write a program that uses input to accept two numbers from the user, then adds them together and reports the result.
d. What do you suppose happens if we try to "add" a string and a number? Try the following short program to see.
print "This song is just " + 6 + " words long."
You got an error message, right? The best we might hope for in this situation is for the number to be concatenated with the strings. However, again because numbers are inherently different than strings, Python can not do this. Yet, as the example suggests, there are times when we would like to do so.
In such a situation, we can use a function called str to convert the number to a string containing the same digits. (Thus, the function str is essentially the opposite of the function int, which you just learned.) Please try the following example yourself to see that it works.
print "This song is just " + str(6) + " words long."
e. Write a program called graduate.py that asks the user to enter the year he or she expects to graduate. Then print a message that includes this information. (Your program should use input to accept the input data, so that you can exercise your knowledge of the function str.) An example run of your program might look similar to the following.
What year will you graduate? 2010 You will graduate in 2010? Congratulations!
f. Now see if you can modify your program graduate.py, so that its output message is like the following. Note that to do so, you will need to subtract two numbers, which can be done just as you would probably expect.
What year will you graduate? 2010 Congratulations! You have only 1 more year to go!
Finally, try entering different years when you run this program. Does it work correctly for any year on or after 2009? If not, please modify your program so that it does.
a. The multiplication operator in Python (and other programming languages) is an asterisk. For example, we might say
product = 2 * 3
Write a program that asks the user to enter two numbers and a word. Your program should then multiply the two numbers together and report the output. It should also multiply the word by one of the numbers and report the output. Do you get the result that you expected?
b. Write a program that asks the user to think of a month and then enter the number of days in that month. Your program should then compute the number of minutes in the month, and report the result. An example run of your program might look like this:
How many days are there in your month? 29 Your month has 41760 minutes.(Hint: Start by thinking about how many hours there would be in the month.)
c. In this exercise, you will write a program called imagesize.py that computes the number of bytes required to store an (uncompressed) image. You should assume that we need 4 bytes to store each pixel, and you should ask the user for the width and height of the image (in pixels). An example run of your program might look like the following.
Image width (pixels): 800 Image height (pixels): 600 File size: 1920000 bytes
a. The only numbers we have used in the examples so far have been integers, but we can work with floating-point numbers in Python as well. (Remember that the term floating-point describes the way we represent real numbers in a computer.) We use the function input to accept floating-point numbers from a user, just as with integers. Python will recognize a number as a floating-point number if it contains a decimal point; otherwise, the number will be recognized as an integer.
For example, if the user chooses to answer the prompts in the following way, Python will consider the first number an integer and the second one a floating-point number.
Please enter a number: 5 Now another number: 5.0
Why should we care? One reason is that the division operator gives a different result when we divide two real numbers than it does when we divide two integers. For example, the result from (5 / 2) will differ from the result given by (5.0 / 2.0). This may seem surprising at first, but consider that it is similar to the fact that the addition operator gives a different result when we add two numbers than it does when we "add" two strings. It is a way to make both of these useful operations available to you, the programmer.
Write a program that accepts two numbers from the user, then divides the first one by the second one and reports the result. Then experiment by running your program and entering different numbers. Try to determine the answers to the following questions:
We call the operation of dividing two integers integer division. If you are still not completely sure what it does, here is a hint. I know a Prof who claims we learned it in elementary school as the "gazinta" operator: it tells us how many times one number goes into another (evenly). So ( 5 / 2 ) can be restated as "2 goes into 5 two times."
b. In this exercise, you will write a program called candy.py that could be used by a kindergarten teacher to help with the following problem. The teacher brings a bag of candy for the class, and s/he must make sure that all students get the same number of pieces.
Your program should prompt the teacher to enter the number of students who are present that day, and also to enter the number of candy pieces available. Your program should then report the number of pieces each student should be given.
Once you have that working, modify the program so that it also prints the number of pieces that will be left over for the teacher.
Once you have the second part working as well, you may or may not be pleased to learn that there is another operator you can use to compute the remainder in an integer division problem. For example, the program below should compute and print the remainder after removing as many 2's as possible from 5.
remainder = 5 % 2 print remainder
Unless you used this operator already, modify your program to use it when computing the amount of candy left over for the teacher.
c. Return to your program called imagesize.py. Modify it so that after printing the number of bytes needed to store the image, it also reports the number of kilobytes needed.
Hint: Remember that one kilobyte is equal to 1024 bytes, so you want to divide the number of bytes by 1024.
An example run of your program might look like the following:
Image width (in pixels): 800 Image height (in pixels): 600 File size: 1920000 bytes That is: 1875 kilobytes
d. Write a program that prompts the user to enter a temperature in Fahrenheit, and reports the equivalent temperature in Celsius. The conversion equation is:
A sample run should give you the following result:
Enter degrees fahrenheit: 100 Celcius degrees: 37.7777777778
Write a program called change.py that asks the user to input the amount of change your program is to make, and then compute the number of quarters, dimes, nickels, and pennies that produces the correct change with the fewest coins possible.
A sample run of your program might look something like this:
Enter the total change: 83 Change: 3 quarters 0 dimes 1 nickels 3 pennies