Feel free to take a look at this tutorial in advance, but since attendance is required this first week so you can meet your team, you do not need to actually work on it until class on Wednesday.
Submitting
Regardless of how you choose to complete the assignment, you MUST submit the file you worked on to Canvas. Do that BEFORE LEAVING THE ROOM if you are in-person.
IF YOU ARE SUBMITTING REMOTELY
This is not an option for this first tutorial. Come meet your tutorial team. In future weeks, you can choose to do this and your file will be graded for completeness and correctness.
IF YOU ARE IN CLASS OR AT AN ALT TUTORIAL
Then your file will be graded for participation (i.e. it’s okay if you haven’t finished the entire thing because you may have spent time asking a question; helping a friend; etc.) along with what we call a ‘peer review.’ Find one other person in your group that is finished and peer review each other’s work. Here are the things to check:
- Does their code look readable and neat?
- Can you understand what their code does by reading it?
- How was their solution different from yours (if at all)?
- Does their program run and generate the correct results?
Once you’ve debriefed, both of you should fill out this attendance Google Form. NOTE: You will need the NetID of the person's whose code you reviewed. This form will only be available near the end of the tutorial session.
You’re free to go after you’re finished and submitted both your .py
and Google Form, though we hope that you might consider sticking around and helping others in your group.
Getting Started
Our objectives for this assignment are:
- Storing data in variables
- Doing basic math in Python
- Understanding data types
- Calling basic functions
Part 0. Meet Your Group
A first step to building any community is to introduce yourselves!
Once in your group, figure out who has the earliest birthday (i.e. January 1st is the earliest possible birthday). This person will go first and introduce themselves with the following:
- Name
- Major
- Year in School
- Favorite Breakfast Cereal (or other delightful breakfast delicacy)
Note, your group will have a Peer Mentor or TA assigned to it and they will come introduce themselves as well! They’ll be easy to identify as they’ll all have name tags! Because PMs are assigned to multiple groups, they may not be present for all the intros and might ask you to quickly go around and say names again.
Once you’ve all had a chance to introduce yourselves, you can get started on your tutorial!
Part 1. Get Your Files Organized
Create the folder organizational structure recommended in class during Monday’s Lecture.
cs110
|-- homeworks
│ |-- hw0
│ |-- hw1
| ...
|-- lectures
| -- lecture00
│ -- lecture01
│ -- lecture02
│ ...
|-- tutorials
...
Part 2. Create two Python files and Write Some Programs
When you’re done, open IDLE and create a new python file (go to File menu > “New File”). This should open a blank document. Then save this blank document as grade_calculator.py
(File menu > “Save as…”), and put it inside of your cs110 > tutorials > tutorial1
folder (You’ll need to make that tutorial1
folder just like you did with the three top-level folders).
In this newly created grade_calculator.py
file, you’re going to write a program to calculate your grade in our class:
Storing Data in Variables
- First create a new variable called
miniquizzes
and store in it the number100
Hint!
We'll help you on this first one in Python. Copy and paste the below into your file:In plain English this says: "Please assign the value
- Next, create a new variable called
projects
and store in it the number95
. - Next, create a new variable called
exercises
and store in it the number95
. - Next, create a new variable called
tutorials
and store in it the number100
. - Finally, create a new variable called
quizzes
and store in it the number90
.
Make sure to run your program by going to the Run
menu at the top of the screen and selecting Run Module...
. If you don’t see any red error text, that means your program successfully ran! Since we haven’t asked Python to actually output anything yet (we’ve only asked it to remember some numbers) we won’t see any output.
How do I turn on line numbers?
Ever notice that in my IDLE you can see a number before each line? You can make IDLE do the same for you by going to the OPTIONS menu and selecting Show line numbers.Accessing Data in Variables
Accessing data stored in a variable is as easy as using the name of the variable in another line of our program. Python will then see this name and lookup what value you’ve asked it to remember is stored inside that container.
Now let’s actually calculate the course average for these 5 inputs. Recall from our syllabus that your grade is broken down in the following categories.
Tutorials | 10% |
Mini-Quizzes | 5% |
Exercises | 25% |
2 Projects | 25% |
3 Quizzes | 35% |
What's a weighted average?
When you see a weighted average like this, it means that different assignments are weighted by their respective percentages (i.e. a category with a higher weight means it constitutes a larger part of your final grade). To calculate the final weighted average, we take the decimal form of the weight for each category and multiply that weight by the average for that category, after doing that for each category, we add up all of those calculations and that gives us our final grade.For example, to calculate the part of your grade determined by the tutorials, we would take the tutorial weight in decimal form (10% -> 0.1
) and multiply it by the overall tutorial score. Then you’d repeat this process for each different assignment category. Imagine you got a 100
for all 5 categories, that would mean your final grade would be calculated by saying:
Grade Calculation Equation (0.10 * 100) + (0.05 * 100) + (0.25 * 100) + (0.25 * 100) + (0.35 * 100)
However, we need to use the grades that are stored in the variables we created earlier. For example, the overall tutorial score is scored in the variable tutorials
. To access the actual value of that variable, we simply use its name (tutorials
) in place of an actual number. For instance, to calculate the part of your grade determined by the tutorials, in Python we’d say:
(0.10 * tutorials)
That says multiply 0.10
with whatever value is stored in the variable tutorials
. When you go to run your file, Python will then lookup whatever number is stored in that container and use it for the calculation.
- Use this example to modify the Grade Calculation Equation above so that is uses the variables you created earlier rather than values of
100
. - Store the result of this calculation in a variable called
grade
- Run your program! (if you run into an error (i.e. red text or a popup saying it can’t run) raise your hand to ask for help!)
Using the print
Function
Well that was a let down…nothing got outputted!
Remember: computers don’t do anything we don’t ask of them. Because we didn’t ask Python to output the value of grade
, when we run our program…it doesn’t output the result of the calculation.
Add a new line to your program that calls the print
function with an argument of the variable grade
. Now try running your program again. If everything is correct with your calculator, you should see 94
printed out in the Interpreter (>>>
) Window since we’ve asked our program to output the value stored in that variable.
Using the input
Function
What we’ve done now is essentially asked the computer to remember the average of our scores across these 5 different types of assignments and then calculate the resulting course average. Now, these variables are currently “set in stone” or hardcoded to be particular numbers. In other words, every time we run this program, it will always use those exact same numbers for the grade calculation. While that’s fine when calculating our grade, what if we wanted to be able to send our program to a friend in the class so they could calculate their grade without modifying the code?
Using the input
function, we can tell Python to ask the person running the program to type a number using keyboard. The input
function takes a string
as an argument which represents the prompt Python shows the user and returns a string
of whatever the person typed in.
- Modify the
quizzes
variable so that instead of being assigned the value of 90, it is instead assigned the value of running theinput
function.
Hint!
In other words...In plain English this says: "Please assign the value of whatever we get back from calling the
- Now try running your program.
Did it work? No? Uh oh. Let’s think about why by using the error we got as a clue:
Traceback (most recent call last):
File "/Users/username/Desktop/tutorial1/grade_calculator.py", line 7, in <module>
grade = (0.10 * tutorials) + (0.05 * miniquizzes) + (0.25 * homeworks) + (0.25 * projects) + (0.35 * quizzes)
TypeError: can't multiply sequence by non-int of type 'float'
This error says it occurs on the line where we calculate the final grade
and that it says it’s a TypeError
. In other words, Python says you asked me to multiply the value stored in this variable…but it’s not a number! Why might that be? Well remember that the input
function returns a str
or string
. But the *
operator only knows how to deal with numbers! That means we need to convert or cast the value we get back from input
from a str
to an int
(or integer).
To convert the quizzes
variable to an integer we need to use the int
function which accepts any argument and will try its best to return an int
(integer) from that argument.
There’s two (easy) ways you can implement this in your program.
- You could add a new line after the line that says
quizzes = ...
that saysquizzes = int(quizzes)
– in other words, you’d ask Python to replace the existingquizzes
variable with the result of converting whatever is currently in thequizzes
variable to anint
. - You could change the calculation of
grade
to useint(quizzes)
rather than justquizzes
– this essentially tells Python to not modify the original variable but to instead just convert the value for the purposes of the calculation.
Pick an approach and implement in your program. Finally Run your program again and make sure you get the expected output given whatever you typed in! In fact…run it twice with two different numeric pieces of data (note, it will only work with whole numbers) and make sure you get two different final grades!
Part 3. Turtle Time
This time, rather than starting from scratch, we’re going to give you a template to work in. Click the big purple button below to download the file turtorial.py
. Move that downloaded file into your tutorial1
folder and go ahead and open it in IDLE.
The file isn't downloading. What do I do?
Some Web Browsers don't automatically downloadTurtle Function Cheatsheet
### TURTLE CHEATSHEET ###############################################
# Pretend we have a turtle named: turtle_0
# If we want turtle_0 to go forward 100 steps we just say:
forward(turtle_0, 100)
# If we want turtle_0 to turn left or right 90 degrees, we just say:
left_turn(turtle_0, 90)
right_turn(turtle_0, 90)
# If we want to turn turtle_0 around, we'd just turn 180 degrees!
right_turn(turtle_0, 180)
# If we want turtle_0 to change the color of its pen:
change_pen_color(turtle_0, "green")
# If we want to make a new turtle at a specific x, y coordinate, we use the optional
# arguments to the MyTurtle Function like so
turtle_0 = MyTurtle(x = 100, y = 100)
# (If you leave those out, it will default to 0, 0)
Now here’s your job:
- Modify the program so that your turtle draws a square.
- Modify the program so that after drawing a square the turtle…
- first changes its pen color to
"cyan"
- then draws a rectangle (i.e. a shape with non-equal sides)
- first changes its pen color to
- Finally, depending on how much time is left, make a SUPER COOL design. What sorts of shapes are easy to draw? What sorts of shapes are difficult to draw?
Some Pro Tips
- Always save your python file before running it so that the interpreter “sees” your changes.
- In order for your interpreter to recognize your file as a Python file, you must give it the
.py
file extension (e.g.my_program.py
). - Don’t forget to name all of your variables “snake case” (all lower case, underscores to separate words).
- If your code doesn’t run, practice reading and trying to understand what the interpreter is telling you. If you see red error text, the computer is trying to tell you where the problem is.