Starting from:
$30

$24

Lab Assignment #2 Solution

Assignment Objectives




By the end of this lab you should be able to write short Python functions that use if-statements and relational operators to solve simple problems.










Part I: Compute the Value of a Formula (20 points)




Write a function compute2() that takes the following arguments, in this order:




1. x: the first value you need for the formula




2. y: the second value you need for the formula




Your function should return the value of the formula given below (using floating-point division):

x + 2y x ≤ 0





 1 − y

f (x, y) = 

x



0 < x ≤ 8


 y2

x3 x 8







Examples:




Function Call
Return Value
compute2(6.64, -5.02)
0.9066265060240963
compute2(-9.15, 6.53)
3.91
compute2(9.66, 3.8)
0.016019015218925313
compute2(-7.83, -9.59)
-27.009999999999998
compute2(-9.01, 7.45)
5.890000000000001
compute2(-2.37, 1.62)
0.8700000000000001
compute2(-0.9, 0.22)
-0.46
compute2(9.08, 2.33)
0.00725194157380947
compute2(5.56, -3.61)
0.829136690647482
compute2(6.83, -4.12)
0.7496339677891655
Part II: Computer Price (20 points)




Write a function computer price() that takes the following arguments, in this order, and computes the price of a computer:




1. cpu: the manufacturer of the CPU, which must be “Intel” or ”AMD” (capitalized just so)




2. ghz: the gigahertz rating of the CPU, which must be a positive number




3. disk type: the disk type, which must be “SSD” or “HDD” (capitalized just so)




4. disk size: the disk’s size in gigabytes, which must be a positive number




The base price of the computer is $1000. Components add to the computer’s price as follows:




• An Intel processor adds $200.




• An AMD processor adds $175.




• A CPU running at 2 GHz or higher adds $150.




• A CPU running at 1 GHZ or higher (but less than 2 GHz) adds $80.




• A solid state disk (SSD) adds $225.




• A hard disk (HDD) adds $100.




• Each gigabyte of disk space costs $2.




Based on the computer’s specifications, the function computes and returns the computer’s price. If any of the four arguments is invalid, the function returns the floating-point number −1.0.




Examples:




Function Call
Return Value
computer price(’AMD’, -0.1, ’hdd’, 16.7)
-1.0
computer price(’Intel’, 0.0, ’SSD’, 86.0)
-1.0
computer price(’AMD’, 3.5, ’HDD’, 53.8)
1532.6
computer price(’Intel’, -0.1, ’HDD’, 44.5)
-1.0
computer price(’AMD’, 1.4, ’HDD’, 1.8)
1358.6
computer price(’Intel’, 2.8, ’SSD’, 62.0)
1699.0
computer price(’intel’, 1.1, ’SSD’, 82.5)
-1.0
computer price(’AMD’, 3.3, ’SSD’, 63.9)
1677.8
computer price(’AMD’, 3.4, ’HDD’, 48.8)
1522.6
computer price(’Intel’, 2.3, ’ssd’, 97.9)
-1.0






Part III: Tracing Code Execution (10 points)




Note: the skills needed to complete this part of the lab assignment will be demonstrated in lecture on February

6th.




This problem is different from those you have done before. In this problem you will use the PyCharm Debugger to trace code and record in Blackboard the value of a variable at various points during execution of the code.

Begin by downloading company.py from Piazza. In this file you are given a completed function called

company policy() that returns a string. The function takes these arguments:




1. experience: an integer that provides a measure of a person’s experience




2. base pay: an integer that represents the base pay of a person




Visit Blackboard and navigate to the “Assignments” section. Find the link marked “Lab 2 Part 1” and click on it. You will find five questions to answer. Now note the following lines towards the bottom of company.py:




experience_test = 18 base_pay_test = 26000




In order to answer the questions in Blackboard you will need to edit the lines noted above to change the arguments as indicated in the question. As an example, here is the first question on Blackboard:







Suppose experience test = 11 and base pay test = 10000. What is the value of result

before the line marked Label 10 is executed?







In PyCharm, we would locate the line of code marked “Label 10” and click in the empty region just to the left of the line to create a breakpoint. (Recall that a breakpoint causes PyCharm to pause execution of a program at a line of code so that the programmer can inspect the values of variables.) We would then use the Run menu and select “Debug company” to start the debugger. See the Unit 3 lecture notes for more information about how to use the debugger in PyCharm.




Provide the answers to the following questions in Blackboard:




1. Suppose experience test = 11 and base pay test = 10000. What is the value of result

before the line marked Label 10 is executed?




2. Suppose experience test = 4 and base pay test = 10000. What is the value of result

before the line marked Label 4 is executed?




3. Suppose experience test = 4 and base pay test = 10000. What is the value of result

before the line marked Label 15 is executed?




4. Suppose experience test = 8 and base pay test = 26000. What is the value of result

before the line marked Label 8 is executed?




5. Suppose experience test = 18 and base pay test = 26000. What is the value of result

before the line marked Label 13 is executed?







How to Submit Your Work for Grading




To submit your .py file for grading:




1. Login to Blackboard and locate the course account for CSE 101.




2. Click on “Assignments” in the left-hand menu and find the link for this assignment.




3. Click on the link for this assignment.

4. Click the “Browse My Computer” button and locate the .py file you wish to submit. Submit only that one

.py file.




5. Click the “Submit” button to submit your work for grading.







Oops, I messed up and I need to resubmit a file!




No worries! Just follow the above directions again. We will grade only your last submission.




Note: Files uploaded to CodeLoad will not be graded! You must submit your work to Blackboard for grading.

More products