Computer Science 101

advertisement
Computer Science 101
Python Lab 2
Project 2 Folder:
Go to your MyPython folder on your H drive and create a new folder called Lab2.
This is where you will save your completed programs for this lab.
Program Header:
For each of the programs, you should begin with comments at the top, giving your
name, the name of the course, the name of the program and a brief description of
the purpose of the program. Also, it is important that you name your programs
exactly as specified so that I will know where to find your work. Be sure to use the
extension “.py” for all of your Python programs. All of the programs are done by
defining functions in JES. In all of the functions, use requestInteger and
requestString for input, and use printNow for output.
The Programs:
Program 1 – Order
This program should be saved as Order.py. You should write a function
called order that has the user enter two numbers. The function should then
print out the two numbers in increasing order, together with messages:
The smaller number is: 5
The larger number is: 10 (for example).
If the numbers are equal, treat that common value as both the larger and the
smaller.
When you have this working, modify it to work with three input numbers.
The smaller number is: -6
The middle number is: 8
The larger number is 16 (for example).
Program 2 – Change
This program should be saved as Change.py and the function should be called
getChange. This program should use only whole numbers (type int). You
should not use if-statements or loops. You may use subtraction only once, and
you should not use addition or multiplication. So mainly you should work
with / and %.
When executed the user should enter the cost (in cents) of an item. Assume
that the cost is less than a dollar and the customer pays with a dollar. The
output should be, along with messages, the number of quarters, dimes, nickels
and pennies that should be returned (fewest coins).
Note: Your one use of subtraction is to get the number of cents to return.
Hint: Determine quarters first and then see how much change remains to be
given, etc. (Get it? remains)
Program 3 – Explore media file
This program should be saved as Explore.py and the function should be called
pickAndExplore. When executed, the function should allow the user to pick a
file. If the filename ends in “wav”, the function should make a sound and then
“explore” it. If the filename ends in “jpg”, the function should make a picture
and explore it. Otherwise, the function should do nothing with the file.
Note: If filename is a variable holding a string value, then we can use
filename.endswith
to determine whether or not filename ends with a given suffix. For example, if
filename has the value “Paper.doc”, then
filename.endswith(“doc”) will be true and
filename.endswith(“wav”) will be false.
Note: There are many wav and jpg files in the mediasources folder in the
shared folder in our course folder on the L drive.
Program 4 – Sum
This program should be saved as Sum.py and the function should be called
sumStats. This function should allow the user to repeatedly input whole
numbers. The input is terminated when the user inputs a zero. At that point,
the program should tell (with messages) how many numbers were entered, the
sum of the numbers, how many of the numbers were positive, the sum of the
positive numbers, how many were negative, and the sum of the negative
numbers.
Note: The function should not ask whether the user wants to continue or not.
The prompt for input should indicate that zero will terminate the process.
Note: You should write and test you function piece by piece. First get it to
sum the numbers entered. Then add the feature to count how many numbers
are entered, etc. That way it will be much easier to find mistakes. The lab
assistant and I are expecting you to proceed that way. So if you need help, you
should be able to tell us how much was working correctly.
Program 5 – OneIsZero
One Is Zero is a simple game played with a single die. When your turn comes,
you begin with 0 points for the turn. You roll the die and receive the number
of points shown on the die (unless you roll 1). You may then choose to roll
again and accumulate points according to the die. Your turn ends when either
you choose to quit, keeping the points you have accumulated, or you roll a 1.
If you roll a 1, your score for the turn is 0.
For this exercise, you should copy the file OneIsZero.py from the shared
folder to your Lab2 folder. There you will find my rollDie function that you
should use for simulating the roll of a die. Also there is a skeleton for a
function playTurn that you should complete. This function should let the user
play a single turn of the One Is Zero game and display the number of points
for the turn. So the function should simulate rolling the die and report the roll
value and the number of points. The user should then tell whether or not to
continue. As long as the turn goes on, the function should tell the value of the
roll and the number of points. Whenever the user decides to stop or rolls a 1,
the function should display the number of points for the turn.
Note: We may work with this game more in subsequent labs.
Turning in your work
When you have finished, you should turn in printed copies of all of your programs.
Copy your Lab2 folder and paste it into your returns folder. Be sure you have the
comments at the top as instructed. Indicate whose returns folder contains the
electronic copy.
Download