homework assignment 2. scroll down in the file
CPSC 231 Tutorial Exercise 2 and Homework Assignment 2 Examining Trends of Birth Rates (Learning Objective: understand the use of loops) Tutorial Exercise 2 Weight: 1.25% of final grade Submission: Nothing Demonstrate your code to your tutorial TA. You may demonstrate as many times as you wish until you get it right. Your TA can provide feedback each time you demonstrate. To receive marks for the exercise, you must demonstrate to your TA before the end of your enrolled tutorial session on either Wednesday October 12th or Thursday October 13th. Your TA is not responsible for marking your code in an email. Detailed Descriptions The birth rate is one of the most basic and important measures in demography. Low birth rates can have major impacts on the future of a society – without enough people being born, you either have to accept huge number of immigrants to keep the economy operating, or you have to replace the work that humans did with robots or AI. In this exercise, you will visualize the trend of birth rates in a country. Step 1: Create a new py file to work on. Step 2: Download the new turtlesetup_plot.py file from D2L, and place it in the same directory as your own py file. Step 3: Do not change anything in turtlesetup_plot.py. Your own file should start with from turtlesetup_plot import * Your own file should end with t.hideturtle() screen.exitonclick() # No more turtle.exitonclick() This allows you to start using the turtle in the intended way. The turtle is renamed as t to avoid any further confusions from assignment 1. No more turtle in the code, only t. To move the turtle forward by 100, you would use t.forward(100). You are only allowed to import turtlesetup_plot, not any other imports. You can define your own custom functions for this assignment, but it is not necessary. You can use anything else that is not forbidden in this document. Step 4: For the tutorial exercise, your task is to plot a few points of birth rates on the graph. The program starts by asking the user how many data points they want to enter. Assume the user enters an integer. The program then asks the user to enter each data point, based on the number the user wanted. Assume Year is entered as an integer and Birth Rate is a float. As an example: Year Birth Rate in Canada 1960 3.81 1970 2.26 1980 1.74 See screenshot for the exact output: And the program plots them as a line graph: Demonstrate to your TA that you have done this work. Homework Assignment 2 Weight: 6% of final grade Due date: Friday October 21, at 11:59pm Estimated time needed to complete Assignment 2: about the same as Assignment 1 Submission: one .py file, submit on the D2L Dropbox. You may submit as many times as you wish, and only the latest submission will be marked. Late Submissions: Submitting on October 22nd will use up one personal day. Submitting on October 23rd will use up two personal days, etc. You have a total of 5 personal days for the entire semester. No penalties for using personal days. An assignment will not be accepted if it is late and all personal days are already used. Academic Integrity: This work must be completed individually. Please follow academic integrity rules as discussed during lecture. You may discuss your ideas in English (not in Python) with other students as much as you like, but make sure that when you write your code that it is your own. A good rule of thumb is to never let anyone else see your code, except your instructor and TAs. Detailed Descriptions Continue working from your tutorial exercise file. Step 1: Let’s add some error checking. • When asking the user how many data points they want to enter, assume the user enters an integer. However, program should check to make sure this number is greater than 0. • When asking the user to enter a year, assume the year is entered as an integer. However, program should check to make sure the year is between 1910 and 2022 (including 1910 and 2022). • When asking the user to enter a birth rate, assume it is a float. However, program should check to make sure the number is between 0.2 and 4.5 (including 0.2 and 4.5). • When asking the user to enter a year as part of the data point entry, program should check to make sure the years have been entered in chronological order. You can’t enter 1980 before 1970, for example. • When asking the user to enter a year as part of the data point entry, program should check to make sure the year has not been entered before. You cannot have the same year entered twice. With any one of the above checks, if the user enters something that violates the rule, the program should immediately output “Error.” Nothing else should run (the program stops asking for additional data points) and the program stops when the user clicks on the graph. The auto- grader will look for this exact word with a period to determine if your program is behaving correctly. Step 2: Let’s handle some data cleaning. If the user entered a birth rate that is more than 2 decimal places, round the number to only 2 decimal places (e.g. 3.439 becomes 3.44). Step 3: Let’s create markers. It is very difficult to see where the data points lie without clear markers. You will draw a shape as a marker. You may use whatever shape you wish as the marker – square, star, triangle, circle, etc. Here is one example with little squares as markers: You will need to draw the shape yourself – there is no code to give you an automatic marker. Step 4: We want to have some analysis of the data. One question to ask is, if we know the birth rate in two separate years, can we guess what the birth rate might be in between the two years? Year Birth Rate in Canada 1960 3.81 1964 ? 1970 2.26 1980 1.74 How do we guess what the birth rate was in 1964, given the chart above? This requires interpolation of data. We use the line connecting the two closest points (1960, 3.81) and (1970, 2,26), and based on it, assuming a linear relationship, figure out what the birth rate might be in 1964. After drawing the graph in step 3, add t.hideturtle() to hide the turtle if it hasn’t been done. Do not clear the graph and do not close the graph window. The program then asks the user to enter the two years they want to use to interpolate. Assume the entered years are integers. Do other error checking as step 1 right after the user enters each year. If either year does not exist as an existing data point, the program should output the following message and exit when the user clicks on the graph: The entered data point does not exist. The auto-grader will look for this exact sentence to determine if your program is behaving correctly. Step 5: Based on the user entered years, the program will interpolate and produce an estimate for every year in between the two. The birth rates should be rounded to 2 decimal places when outputting to the screen. The format of the output should be one data point per line: the year, one space, the birth rate rounded to 2 decimals, end of the line (the auto-grader will use this exact format to check your output for correctness.) Do not include the years at the two ends (e.g. if between 1960 and 1970, do not include 1960 and 1970.) Step 6: Finally, plot these estimated new data points on the graph: Click the graph to exit the program. • If you ever wonder how we can predict a future year birth rate, not a birth rate between two known years – this would require machine learning (artificial intelligence). • There are many more data points we can find on birth rates. There may be interesting trends! https://datacommons.org/tools/timeline#&place=country/CAN&statsVar=FertilityRate_Person_Female Grading For tutorial exercise: Grade Point Letter Grade Guidelines 4 A All correctly demonstrated to TA 3 B Mostly correct with minor errors 2 C Major errors 1 D Barely started code and no data points drawn 0 F Did not demonstrate to TA For homework assignment: Grade Point Letter Grade Guidelines 4 A+/A Fulfill all assignment specs, or only one mistake that is not an error 3.7 A- Two mistakes/one to two errors, or incomplete tasks 3.3 B+ Three to four mistakes/errors, or incomplete tasks, or code crashes on occasions 3 B Five to six mistakes/errors, or incomplete tasks, or code crashes on occasions 2.7 B- More than six mistakes/errors, or incomplete tasks, or code crashes on occasions 2.3 C+ Major mistakes/incomplete tasks and code crashes a lot 2 C Code draws some data points but crashes every time, or use other disallowed imports 1.7 C- Data points incomplete and code crashes every time 1.3 D+ Code crashes every time and nothing else is shown 1 D Code does not run due to syntax errors 0 F Barely started code, or no submission, or late submission with no personal days left The assignment will be graded out of 4, with the grade based on the program’s level of functionality and conformance to the specifications. • If the program does not run due to syntax errors, it will not get more than a D grade. • A program that runs but