In this project, you'll write your first program to solve problems from the first three programs in the class textbook.Part 0, 0 points: In this course, you'll be writing and running code natively on your own computer. I made this video for IDLE, which is probably the easiest way to write and run Python programs. Feel free to reference it as you need it during the course of the class, but please only watch during class if you have headphones to use. When running IDLE, make sure you open Python 3 (3.anything) instead of Python 2.
Part 1, 1 points: When you submit your code, you're going to submit it all in one file. Your filename should be <yourschoolusername>_projects.py
Download and open IDLE and create that now. If you need any help, just let me know! Create a new file with the appropriate filename for you. For many of these first problems, I'm going to give you some code, which you'll need to paste into your file, then modify. The code that you change will be inside of functions, which is something we'll do a lot of in this class. I'll show you how it works. Copy the following code and paste it into your projects file:
def hello_world():
print("I don't know how to program yet.")
Save your file, then run (F5) it. Nothing exciting should happen, but now you can execute the code inside of that function. At the chevron, type this:>>> hello_world()
I don't know how to program yet.
>>>
Modify the code so that instead it does this:>>> hello_world()
Hello, World!
>>>
Once you get that working, before moving on to the next part, know that I am going to test your code very carefully to make sure that it does what I want. You can run a part of my tests by downloading this testing code here: Tester. Save that file to the same directory as your project file. Open it in IDLE. If you try to run it, you'll get an error, because of this line:import student_X as main
Replace the student_X
in that line with your own file name (not including the .py). Then try running that file. If nothing happens, your code passed the test. If there's a message, then something went wrong and you need to modify your code. I am a bit of a stickler for getting the exact code to work! (Feel free to call me over if you can't figure out what went wrong.) Everytime you fix your code, you'll need to rerun my testing code to see if you got it to work right. I recommend fully completing each part before you move on to the next, but in general that's a judgment call on your part.
Part 2, 1 points: Big Brother wants us to believe that 2 + 2 = 5. Copy and paste the following code into your file and then fix it to print out the correct equation. Important: Don't overwrite or delete your previous function(s)! When I grade it, I'm going to grade all of them! (Also, don't modify the first line of any of these.)
def two_plus_two_equation():
print("2 + 2 = 5")
Just like before, you can try running this by typing out the name in the first line, with the parentheses: >>> two_plus_two_equation()
2 + 2 = 5
>>>
Fix the print statement so that it correctly prints the sum of two and two. You can test your solution with this code (remember to change the name of student_X to your filename): Tester. (You can absolutely delete these tester files after you run them. If you ever need them again, just come here and download it again.)
Part 3, 1 points: Do the same as in the last one, but using printing with multiple pieces in the print statement, separated by commas:
def two_plus_two_statement():
print(2, "plus", 2, "is", 2+3)
You can try running it to see what the output looks like: >>> two_plus_two_statement()
I recommend keeping the plus sign in there. You can test your solution with this: Tester.
Part 4, 0 points: The next few parts involve making mistakes on purpose, following the Chapter 1 exercises in section 1.9. Try these all out! Don't worry about getting error meessages; you're supposed to make mistakes here! I want you to get used to getting errors!
Part 5, 0 points: Let's cause some more errors! Find out what happens when you misspell a function name. Try executing this line: prnt('Hi there!')
What kind of error does this cause?
Part 6, 0 points: Now let's attempt to add two values that can't be added together:print("3 + 3 is:", 3 + '3')
What kind of error does this cause?
Part 7, 0 points: Now let's see what happens when we try to divide by zero!print(10/0)
What kind of error does this cause?
Part 8, 0 points: In this one, you'll create an error that says: I caused my own exception!
To do this, you create and raise a new Exception, like this: raise Exception(message)
What does it look like when you execute that code??
Part 9, 1 points: For Exercise 2 from the book, I only have exercises for subparts 1 and 2. For 2.1, modify the print statement in this function to be correct:def minutes():
print("There are", 4, "seconds in 42 minutes 42 seconds.")
I expect you'll want to fiddle around in interactive mode to do the calculations first. Alternatively, you can type the expression into the appropriate place in the print statement. After you get it working and in your project file, you can test it with this: Tester.
Part 10, 1 points: For Exercise 2.2 from the book, do the same thing as above, but with the print statement in this function:
def miles():
print("There are", 8/4, "miles in 10 kilometers.")
You can test your code with this: Tester.
Part 11, 0 points: After you have fully read through chapter 2, try out the parts to Exercise 1 in section 2.10. I don't have tests for them, so I recommend trying them out in IDLE.
Part 12, 1 points: The next part asks you to do modify a formula to correctly calculate the volume of a cube. Pay attention to which lines you're supposed to modify and which you're supposed to leave alone:
def cube():
side = 5
volume = side + 10 #Only modify this line!
print("The volume of a cube with sides of length", side, "is", volume)
You can test your code with this: Tester.
Part 13, 1 points: The next part asks you to do modify a line of code to correctly calculate the volume of a sphere. It is totally reasonable to look up the formula online. This is a version of the first part of Exercise 2. Here's the function you'll need to copy and modify:
def sphere():
radius = 5
pi = 3.1415926535897932 #Not exactly pi
volume = radius + 10 #Only modify this line!
print(volume)
Tester.
Part 14, 1 points: Fix the formula to correctly determine the wholesale cost of books, as described in the second part of Exercise 2. Here's the template:def wholesale_cost():
cover_price = 24.95
discount = .4
first_copy_shipping_cost = 3
shipping_cost_per_copy = .75
num_copies = 60
#Only modify the next line (you can also add lines between)
cost = "??"
print(cost)
This is a hard one! I expect you'll have to spend some time coming up with the correct formula. Here's the code to test it: Tester.
Part 15, 1 points: Now we're going to work with printing special characters in strings, notably apostrophes and quotation marks. In this part, you have to print out a string that contains an apostrophe. More specifically, the following sentence: >>> apostrophe()
It's a complete mystery!
>>>
You can start with this function stub if you want (you have to use this function name anyways):def apostrophe():
print()
Use this to check:Tester.
Part 16, 1 points: Now let's use quotation marks. Make a function that does this:>>> quotations()
"I love monkeys," said Kyle.
You can start with this code: def quotations():
print()
Use this to check: Tester.
Part 17, 1 points: In this part, you have to print out a string that contains single and double quotes:>>> both_quotes()
"I'm a monkey," said Kyle.
>>>
Feel free to start with this:def both_quotes():
print()
Here's my testing code:Tester.
Part 18, 0 points: The remaining many parts all expect that you've read through Chapter 3.6. They are all related; you're going to write functions to print out Checker and Draughts boards. I started off with the first function, so add it to your file:
def print_checker_boundary_row():
print("+-+-+-+-+-+-+-+-+")
Part 19, 0 points: The code above is your function definition. If you run the code as-is, it won't do anything. If you want to see what your function does, you have to call it. You can add that function call by putting this in your code: (I recommend adding it at the end.)
print_checker_boundary_row()
Then run the code again to see what's printed out.Part 20, 1 points: Write another function, print_checker_space_row()
, that prints out a row of spaces, separated by vertical lines. (The vertical line character, |
, is known as a "pipe".) Here's a Tester.
Part 21, 1 points: With what you've written so far, you could draw an entire 8 x 8 checkerboard. If we put that in a function, I think it would take 17 lines of code. Before doing that, I decided to create another function named print_checker_spaces_under_boundary()
, which prints out a boundary row followed by a space row. Implement that function yourself. There are two ways to do it: either include the print statements directly, or call the functions above. I want you to do it the second way. Here is code you can use to test your function: Tester.
Part 22, 1 points: Now implement draw_checker_board()
. With the functions we've already written, you should be able to do it in only 9 lines of code. My tester code is here: Tester.
Part 23, 1 points: You might already know that there's a British version of checkers called International Draughts that's played on a 10 x 10 board. Let's go through the same steps to create a function to draw that board: we'll create three new functions. First create print_draughts_space_row
, which is very similar to print_checker_space_row
except that 10 spaces are printed instead of 8. Here is the tester for this exercise: Tester.
Part 24, 0 points: Add print_draughts_boundary_row
and print_draughts_spaces_under_boundary
. These should look similar to the checkers versions.
Part 25, 1 points: Add a new function that draws the entire 10 x 10 international draughts board, draw_draughts_board
. (The body of my version has 11 lines.) Why is this better than just putting all 11 lines into the body of your testing function? Mine looks like this:
+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+
Tester.Part 26, 0 points: Expert Practice: implement a new function, draw_checker_board_with_pieces()
, which draws a starting checker board. I used O
's and X
's for the different pieces. Since I won't grade this, feel free to use a some other common character that makes sense to you. Mine looks like this:
+-+-+-+-+-+-+-+-+
| |O| |O| |O| |O|
+-+-+-+-+-+-+-+-+
|O| |O| |O| |O| |
+-+-+-+-+-+-+-+-+
| |O| |O| |O| |O|
+-+-+-+-+-+-+-+-+
| | | | | | | | |
+-+-+-+-+-+-+-+-+
| | | | | | | | |
+-+-+-+-+-+-+-+-+
|X| |X| |X| |X| |
+-+-+-+-+-+-+-+-+
| |X| |X| |X| |X|
+-+-+-+-+-+-+-+-+
|X| |X| |X| |X| |
+-+-+-+-+-+-+-+-+
Whenever you solve an Expert Practice problem, please brag about it to me!Part 27, 0 points: There might be times where you want to test your code and you can't use IDLE. In that case, you'll want to run python from the command line (preferably powershell or a Mac/Linux/Unix terminal). If you don't want to learn this, you can skip this step. To start, you'll want to be comfortable with using a terminal. I made a terminal tutorial that may help with that. After that feels comfortable, to execute Python from the command line, first navigate to the same directory as your script, then use:
$ python3
>>>
which brings up an interactive mode similar to IDLE's shell. To execute functions from your file, you have to first import it, then run the functions from that package, like this:>>> import username_projects
>>> username_projects.run_tests()
Submitting your Project:
Make sure all your code is in a file labelled with your user name (everything before the @ in your school email address) followed by _projects.py
all in snake_case. (For example, my file name would be: kburke_projects.py
.) It's very important to name your file correctly in order for me to grade it. Make sure your code runs, then upload it to the project on Canvas. (Don't submit code that doesn't run; you won't earn any points!) Your code should include solutions to all non-zero-point problems from Project 0 onwards. If there is already a file up on Canvas, delete that before uploading the new version or make sure your new file replaces that. (Sometimes Canvas adds a number after the file name. Don't worry about that, because it's something (freaking annoying) you don't have control of. I have a script that automatically deletes that.)