Part 1: (10 pts) - University of Delaware



Lab 1:Due Monday, Mar 4, midnightThis is a paired programming lab: In this lab you will work in pairs. In lab, you will choose your partner for the next two weeks. Get your partner’s name and email address. Most likely you will not complete this lab during the allotted lab time and will need to meet with your partner outside of lab. While working on this lab, one person should work the computer and the other person should “navigate”, or coach the computer operator. Every 20 minutes the two partners should switch who is working the computer and who is coaching.When you turn in the lab, both partners should turn in the lab, and the lab must have both students’ names on it. DO NOT FORGET YOUR PARTNER’S NAME. Doing so will result in a penalty of 25%. You should submit your lab via Canvas. The first part of this lab can be completed in an MS Word document, or it can be completed as comments at the top of your lab1.py file. The second part of this lab (involving the python code) should be saved in a python file (with a .py extension) and turned in as lab1.py. The very last part of this lab should be saved in lab1turtle.py. All files should have both partners’ names on them and should be submitted by only one partner via Sakai.Because this is a two week lab, we have not covered all material that is necessary to complete this lab yet. If you complete everything we have covered so far in class, skip down to the turtle section. That section can be done with little to no previous knowledge. Part 1: (10 pts)Attendance week of Feb 17: 4 ptsAttendance week of Feb 24: 4 ptsProblem 1.1 (3 pts) :Python uses atomic data types and builds up from there. Give an example of:a. an intb. a doublec. a stringProblem 1.2 ( 4 pts) : Calculate by hand, then try the formulas using python’s shell. Make SURE you get the same answer both ways. a. 7 + 6 / 2b. 8 /2 ** 3 * 3c. 9 % 5 * -1 // 3 d. 2 * 13 // 5 + 6 / 2 Problem 1.3 (3.5 pts) :Given the following python function and code:def sillyfunc(x): return (x / 5)sillyfunc(3)print (sillyfunc(4))What is the function name?What is the input type?What is the output type?What is the instruction (code) for calculating the function?what does line 3 do?how does line 4 differ from line 3?Once I’ve tested this function, how often can I run the function? Do I have to rewrite it?Part 2: (34 pts)Creating Lab1.py in Python:Below here is where your lab1.py (python) file should start.In the IDLE Shell click on File->New Window. An Editor Window should open.?Copy and paste the expressions from the previous example into the editor window. ?Then choose File->Save As... and save it as lab1.py. ?Run the script by choosing Run->Run Module (or hit f5). ?If you see nothing in the IDLE window, good job! You should see nothing. That means it’s working. Close the file lab1.py. Choose File->Close.Now reopen it. In the IDLE shell, choose File-> Open and locate lab1.py. left326390Note: Every function should include comments. The comments for each function should include:The input type and number of inputsThe output type3 Test cases (given this input, you should get this output)And finally an exact description of what the function does (at this point the functions are fairly simple, so a description of what the function does might be something like “this function calculates x2 + 4”)00Note: Every function should include comments. The comments for each function should include:The input type and number of inputsThe output type3 Test cases (given this input, you should get this output)And finally an exact description of what the function does (at this point the functions are fairly simple, so a description of what the function does might be something like “this function calculates x2 + 4”)With this you can save your work, then come back and edit it later.Problem 2.1 ( 3 pts): Given the following math function, write (as comment) the input type, the output type, the instruction code (in python), and 3 test cases. Now in your newly created lab1.py file, write the function in python. Call it from the main shell with your test cases to make sure it works (use print in your call to be able to see the results):f(x) = (x -3)xPat yourself on the back. You are now writing python functions!!!!Problem 2.2 (4 pts): Given the following math function, write (as comments) the input type, the output type, the instruction code (in python), and 3 test cases. Now in your lab1.py file, write the function in python. Call it with your test cases to make sure it works.g(x,y) = x(y+2)y+1xProblem 2.3 (6 pts): Write 2 separate functions, converting the formulas from problem 1.2b and 1.2d into code. Assume each of the integers in the formula is replaced with an input parameter (so, for instance, in the first formula, there would be 3 input parameters). Develop 3 test cases (as comments) for each of the functions, and test them.Problem 2.4 (3 pts): Including comments, write a function that calculates how much to tip at a restaurant (assuming 21%) and returns that amount. Then test it using your test cases.Problem 2.5 (3 pts) Including comments, write a function that calculates and returns how many calories you have burned when running. To calculate the number of calories burned, use the following formula:w/2.2 * 7.5 * m/60Where:w is your weight (in pounds)m is the number of minutes you ranProblem 2.6 (3 pts): Write a function that takes as input your age as a double (e.g., 18.5), and rounds your age down to the nearest int and returns that number. Make sure you include comments with 3 test cases, and you test your function.Problem 2.7 (4 pts): Write a function that is just slightly different than the above function. In this case, you will round the age up. Note: you should still use floor division. Figure out how to do this using only the tools we’ve learned so far in class. Make sure you include comments with 3 test cases, and you test your function.Problem 2.8 (5 pts): Calculating your monthly car payments. Write a function (including comments) that calculates your monthly car payments. To calculate your monthly car payments, you’d use the following formula:P (r12)(1-1+r12 -m)Where:P is the principle (the amount you’re borrowing to pay for the car, a.k.a. the amount the car cost), r is the interest rate, m is the number of months in which you intend to pay it off. So, for instance (and yes, this can be one of your test cases), if you borrowed $15,000 at a rate of 7% and wanted to pay it off over 3 years (or 36 months), you’d calculate your monthly payments as follows:15000 (0.0712)(1-1+0.0712 -36)Or 463.16 per month.Problem 2.9 (3 pts): Moore’s law states that the number of transistors that can fit per square inch on an integrated circuit doubles every year without increasing the cost. So say we could fit 32 transistors on an integrated circuit in 2001. Then the following chart should hold true:Year:2001200220032004200520062007…#transistors:326412825651210242048…Either in your lab text file or as a comment, write the input type, the output type, the function’s name, the instruction code, and then test cases for a function that, given the year (after 2001), calculates the number of transistors that will fit on a square inch of integrated circuit. Now in the lab1.py file, write the function and test it with your test cases.Part 3: (39 pts)Problem 3.1 ( 2 pts). Write a function (named no_out) that takes as an input an integer, calculates the square of that integer, but has no output.Problem 3.2 (4 pts) . Explain (in comments in your lab1.py file) why this works in python, but is poor form:def f(x,y,z): return(x * y + (y/2 - x) **x)print (f(4,2,3)) and why this doesn’t work in python:def g(x,y,z): return(x * y + (y/2 -x)**x)print(g(2,6))Problem 3.3 a ( 2 pts). Write a function (with comments) that takes as input two integers x and y and calculates y3+x2x4x-3y33.3 b ( 4 pts). Now write a function (including comments) that calculates:72+(y3+x2x4x-3y3)2y3+x2x4x-3y3You must use function you wrote in 3.3a in the code for this function. Problem 3.4( 4 pts): In problem 2.8, you calculated your monthly car payments given your principle, your interest rate, and the amount of time in which you intend to pay the car off. Use the function you wrote in problem 2.8 to calculate the amount of interest you will be paying.Problem 3.5 ( 3 pts): Given the math function:X3 + 3 if x > 10 first_if(x) = x4 + 7/x otherwise Write the function in python (including comments)Problem 3.6 ( 4 pts). Given the math function:14001756667500(y* 2)3/x if y is less than 5 and x is not equal to 0 second_if(x,y) = (y*3)2/x if y is greater than 5 and x is not equal to 0(y-2)/xif y is equal to 5 and x is not equal to 0 -1otherwise Write the function in python (including comments)Problem 3.7 ( 5 pts). At a used bookstore, different people get different discounts. The rules are as follows:If you are over 65, you get a 40% discount on your total purchase price.If you are over 21 (but 65 or under) you get a 20% discount on your total purchase price. If the discounted price is over $70, you get a 20-dollar discount. If the discounted price is over 50 but 70 or under, you get a 10 dollar discount. If the discounted price is over 30 but 50 or under, you get a 5 dollar discount. Otherwise you just get the 20% discount.If you are over 5 but 21 or under, you get a 30% discount. If your discounted purchase price is over 100, you get another 20 dollars off. Otherwise you just get the 30% discount.Finally, if you are 5 or under, you get a 70% discount on your purchase price (because we want to persuade children to read, of course!Write a function (with comments and test cases) that calculates the amount a person will pay on their purchase at the used bookstore. What are the input parameters? Problem 3.8. We’re going to write a program that calculates a person’s recommended daily calorie intake to maintain their current weight. To do his, we have to first write a bunch of “helper funtions”( 1 pt) Write a function (with comments) that takes a person’s height in inches and returns their height in centimeters ( 1 pt) Write a function (with comments) that takes a person’s weight in pounds and returns their weight in kg( 4 pts)Write 2 functions (with comments) that take as input a person’s height in inches, their weight in pounds, and their age, and calculates a person’s BMR (basal metabolic rate).Note: clearly these two fuctions should both use functions a and b that you just wrote!For women (for the female function) the Basal Metabolic Rate is calculated as follows:447.593 + (3.247x weight in kg) + (3.096 x height in cm) – (4.33 x age in years)For men (for the male function) the Basal Metabolic Rate is calculated as follows:88.362 + (13.397x weight in kg) + (4.799 x height in cm) – (5.677 x age in years)( 5 pts) Now write a function (with comments) that calculates your total recommended daily calorie intake needed to maintain your current weight. This is calculated as follows:If you get no exercise: BMR x 1.2If you get light exercise: BMR x 1.375If you get moderate exercise: BMR x 1.55If you get heavy exercise: BMR x 1.725Two hints: you can represent no exercise as the number 0, light exercise as the number 1, moderate as the number 2, and heavy as the number 3.You can also represent Female as 0 and Male as 1.So of your 5 input values to this function, one will hold a number representing whether the person is male or female and one will hold a number representing how much exercise the person gets.Part 4: Intro to Turtle: (9 pts)Turtle is a python library that provides graphics primitives. To use it, open a second file called lab1turtle.py. The very first line of this file needs to “import” the library, or make the library’s functions available to this particular file. So at the top of the file, the first line should be:import turtle(1 pt) Now add the following lines to the file:turtle.forward(50)turtle.right(90)turtle.forward(50)turtle.right(90)turtle.forward(50)turtle.right(90)turtle.forward(50)Now save the file and run it. A separate window should pop up and a square should be drawn. Each time the command turtle.forward(50) is issued, the “turtle” should move forward 50 pixels. turtle.right(90) rotates the direction in which the turtle is going by 90 degrees. Problem 4.1 (3 pts): Write a function below the lines of code (above) that takes as input a number, and draws a square with sides the length of that number. Note: this is an example of a function that does not return a value. So the last line of this function should simply be:return()Turtle colors You can change the colors turtle is using to draw a line. You can use different colors to fill the object you drew as well. Colors on the computer are represented in terms of the amount of Red, Green, and Blue (RGB values). In turtle, to set the color to red, I’d use the following command:turtle.color(1,0,0)To set the color to blue, I’d use:turtle.color(0,0,1)and to set it to green, I’d use the following:turtle.color(0,1,0)Thus the first number indicates whether we have red or not, the second whether we have green or not, and the third whether we have blue or not. We can have more than one color:turtle.color(1,0,1) Means we’ve got red and blue, and will show up as purple.turtle.color(0,1,1) has both green and blue, and results in cyan (blue-green) . And finally,turtle.color(1,1,0) means we’ve got red and green and results in YELLOW! Yep, yellow.Finally, if we’ve got no red, green, or blue, we have black. So the following:turtle.color(0,0,0)Results in black.And if we have all three colors (red, green, and black), we have white. So the following:turtle.color(1,1,1) Results in white.(1 pt) To see colors, try the following (below your last function):turtle.color(1,0,0)turtle.begin_fill()turtle.forward(100)turtle.left(90)turtle.forward(20)turtle.left(90)turtle.forward(20)turtle.right(90)turtle.forward(20)turtle.left(90)turtle.forward(60)turtle.left(90)turtle.forward(20)turtle.right(90)turtle.forward(20)turtle.left(90)turtle.forward(20)turtle.end_fill()turtle.color(0,0,0)turtle.up()turtle.forward(10)turtle.down()turtle.begin_fill()turtle.circle(10)turtle.end_fill()turtle.setheading(0)turtle.up()turtle.forward(90)turtle.right(90)turtle.forward(10)turtle.setheading(0)turtle.begin_fill()turtle.down()turtle.circle(10)turtle.end_fill()Save the file and run it. We’ve got a couple new commands in here:turtle.begin_fill()turtle.end_fill()These commands indicate that the shape we are drawing between the two commands should be filled with the color set in turtle.color.turtle.up() lifts the pen off the page. With this command you can move the turtle around the screen without drawing a line.turtle.down() places the pen back on the screen, so from that point on the pen will draw on the page with each movement.turtle.circle(10) Draws a circle with a radius of 10.And finally, turtle.setheading(0)Sets the heading to 0th degree, meaning, in this case, facing right.Problem 4.2 (4 pts):Write a function that uses input parameter(s) to determine what color to create an object. Within the function, draw an object, and fill it with the color indicated by the input parameter(s). You can draw whatever you feel like drawing – a robot, a flower, a lightbulb, anything you want. Hint on the color: you can either use 3 input parameters, one for red, one for green, and one for blue, or you can use one input parameter that might be 0- 7, with 0 being black, 1 being red, 2 being green, 3 being blue, 4 being purple, etc.To Turn In (via Canvas):Part 1 (either as comments in lab1.py or in a separate MS Word document)Part 2 and 3 in lab1.pyPart 4 in lab1turtle.py ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download