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:

  1. Storing data in variables
  2. Doing basic math in Python
  3. Understanding data types
  4. 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:

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

  1. First create a new variable called miniquizzes and store in it the number 100 (see hint below).
  2. Next, create a new variable called projects and store in it the number 95.
  3. Next, create a new variable called exercises and store in it the number 95.
  4. Next, create a new variable called tutorials and store in it the number 100.
  5. Finally, create a new variable called quizzes and store in it the number 90.
Hint! We'll help you on this first one in Python. Copy and paste the below into your file:

miniquizzes = 100

In plain English this says: "Please assign the value 100 to the variable named miniquizzes".

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 (10% -> 0.1) and multiply it by the overall tutorial score. We'd repeat this process for each different category.
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

  1. Use this example to modify the Grade Calculation Equation above so that is uses the variables you created earlier rather than values of 100.
  2. Store the result of this calculation in a variable called grade
  3. 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!)
  4. print the value stored in grade!
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 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

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

  1. Modify the quizzes variable so that instead of being assigned the value of 90, it is instead assigned the value of running the input function (see hint below).
  2. 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...

quizzes = input("Please enter your quiz average:")

In plain English this says: "Please assign the value of whatever we get back from calling the input function, to the variable named quizzes".

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 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 inputfunction 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).

Action Items

  1. 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 says quizzes = int(quizzes). This tells Python to replace the existing quizzes variable with the result of converting whatever is currently in the quizzes variable to an int.
    • You could change the calculation of grade to use int(quizzes) rather than just quizzes. This tells Python to not modify the original variable but to instead just convert the value for the purposes of the calculation.
  2. Pick one of these approaches and implement it in your program
  3. 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.

Tutorial Starter File

The file isn't downloading. What do I do? Some Web Browsers don't automatically download .py files. If this happens to use, just "right click" (or hold down the CONTROL key on your keyboard and click) on the button and select "Save linked file as..." or "Download linked file..." (the exact wording will depend on your browser - i.e. Chrome, Safari, Firefox, etc.)
Windows 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

  1. Modify the program so that your turtle draws a square.
  2. 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)
  3. 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