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.
In future weeks you will be allowed to submit remotely and have your .py file checked for correctness. That isn’t an option this first week.
If you’re submitting in-class, make sure to check-in (there will be one question called CHECK-IN Q only available for the first 7 minutes of class) and check-out (there will be a three question survey called CHECKOUT SURVEY only available for the last 10 minutes of class) using PollEverywhere with your location verified.
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
miniquizzesand store in it the number100(see hint below). - Next, create a new variable called
projectsand store in it the number95. - Next, create a new variable called
exercisesand store in it the number95. - Next, create a new variable called
tutorialsand store in it the number100. - Finally, create a new variable called
quizzesand store in it the number90.
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
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.
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.
To calculate the part of your grade determined by the tutorials, we would take the tutorial weight in decimal form (
What are the weights?
- Tutorials: 10%
- Mini-Quizzes: 5%
- Exercises: 5%
- Projects: 25%
- Quizzes: 35%
Assuming we got 100s in every assignment category our Grade Calculation Equation would be…
(0.10 * 100) + (0.05 * 100) + (0.25 * 100) + (0.25 * 100) + (0.35 * 100)
But we want to make a calculator for any grade combination so let’s use our earlier variables. For example, the overall tutorial score is scored in the variable tutorials so we replace (0.10 * 100) in the above program with (0.10 * tutorials).
What does this mean in English?
That says multiply `0.10` with the value stored in the variable `tutorials`. When you go to run your file, Python will then lookup the number in that container and use it for the calculation.
Action Items
- 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!)
-
printthe value stored ingrade!
Why do we need to print ?
Remember: computers don't do anything we don't ask of them. Unless we ask Python to output the value of
Using the input Function
At this point we’ve asked the computer to remember the average of our scores across these 5 different types of assignments and then calculate the resulting course average. These variables are currently “set in stone” or hardcoded to be particular numbers. Every time we run this program, it will always use those exact same numbers for the 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 (after they type it in and hit enter on the Interpreter Window).
Action Items
- Modify the
quizzesvariable so that instead of being assigned the value of 90, it is instead assigned the value of running theinputfunction (see hint below). - Now try running your program, making sure to type a number in the Interpreter/Shell Window and then hit the return key on your keyboard.
Hint!
In other words...In plain English this says: "Please assign the value of whatever we get back from calling the
Dealing with Errors
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'
How do we read this crap?
This error says it occurs on the line where we calculate the final
Action Items
- To convert the
quizzesvariable to an integer we need to use theintfunction which accepts any argument and will try its best to return anint(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). This tells Python to replace the existingquizzesvariable with the result of converting whatever is currently in thequizzesvariable to anint. - You could change the calculation of
gradeto useint(quizzes)rather than justquizzes. This tells Python to not modify the original variable but to instead just convert the value for the purposes of the calculation.
- Pick one of these approaches and implement it in your program
- Run your program! Make sure to try different numeric inputs to see you get 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 downloadWindows PC: the file doesn't open
Unfortunately, when you double click on a .py file in Windows, it assumes you want to RUN it rather than EDIT it. You can fix this by following these instructions here instructions here. However, if you don't want to do that, the easy solution is to open IDLE first (search for it using the magnifying glass), click on the FILE menu, then select OPEN. Use the pop up window to navigate and then open the downloaded file.
Turtle 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)
Action Items
- 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
.pyfile 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.