Starting from:
$30

$24

Exercise #2 Solution

This week, we're working with functions and the design recipe. For this exercise, you must add 4 functions to the le ex2.py1. (As always, all le and function names must be exactly as described in this handout). I've left some global variables for you to use in your calculations at the top of the le, feel free to play around with them, (you should be using these in your calculations), but return them to their original values before submitting.







Percentage



Complete a function called percentage, which takes as its parameters two oats 2, and returns the a number representing the rst parameter as a percentage of the second parameter. We've provided you with the rst few steps of the Design Recipe to get you started.







Term Mark



Write a function called term work mark, which calculates your term mark (as a contribution to your nal grade) given your marks on all of the coursework components). Using the default weights this will return a maximum result of 60 (since the exam is worth 40% of your nal grade):







your mark on assignment 0 (by default this is out of 25) your mark on assignment 1 (by default this is out of 50) your mark on assignment 2 (by default this is out of 100) your mark on the exercises (by default this is out of 10)




your mark on the quizzes (by default this it out of 5) your term test marks (by default this is out of 50)3




In the starter code provided in ex2.py, there are already several global variables that you will need to compute this score. The contribution of each piece of coursework is computed as:

mark you received weight

maximum mark




Your coursework mark is the sum of these component contributions. An example output is given below (note that this may vary depending on the state of the global variables):




term_work_mark(25, 50, 100, 10, 5, 50)



60.0




term_work_mark(20, 45, 70, 8, 4, 40)



47.9




Hint:




Rather than writing the same computation over and over, you can make your life easier by using a helper function... perhaps some nice person may have left a useful function in the le before uploading it...







there are already two functions and some global variables in the starter code, you do not need to edit this part of the le in any way



2remember, an int is just a special type of a oat

3I'm not guaranteeing that these numbers will be correct for the real course, that's why I've left the variables there for you to change.




1
Final Mark



Write a function called final mark, which performs a similar calculation to term work mark, but takes an additional parameter, your nal exam mark (be default, out of 100), and returns your nal mark for the term out of 100.







An example output is given below:




final_mark(25, 50, 100, 10, 5, 50,100)



100.0




final_mark(20, 45, 70, 8, 4, 40, 73)



77.1







Hint:




Be lazy! If you use the other functions you've written, this shouldn't take more than 3-4 lines of code.







See If You Pass



Write a function called is pass that takes the same parameters as final mark, and returns a boolean representing whether you passed the course. Remember that in order to pass the course you must get a nal overall mark at or above a certain threshold (by default 50) as well as an exam mark at or above a certain threshold (by default 40).







An example output is given below:




is_pass(20, 45, 70, 8, 4, 40, 41)



True




is_pass(20, 45, 70, 8, 4, 40, 39) False



is_pass(10, 21, 12, 2, 1, 15, 23) False






Hint:




See hint for the previous section. 3-4 lines of code should be plenty. Don't do more work than you need to.







Testing it All Out



Now for the fun bit. On the course website is a le called ex2 gui.py. Put this in the same directory as your ex2.py le and run it. If you've done everything correctly, it should provide a GUI4 for you to test your functions. You don't need to edit the code in the le or even read it (I wouldn't expect you to understand most of it yet anyway). But for those of you who are desperate to build something with buttons... click away!







What to Submit



You should submit your completed ex2.py le and your ex2.test le (with your test cases for all the functions you wrote) to MarkUs. You do not need to submit ex2 gui.py.







Graphical User Interface



2

More products