Python - Week 5 of 6: Functions

Nov 25, 2020 11:38

As before, Week 5 is split into four sections: Introduction, Lecture Materials, Assignment, and Bonus Interviews.  The introduction gives a brief overview of this week's topic and provides the link to Chapter 4 in the textbook and pdfs for the lecture slides.  This week explains what functions are and how to use them.  The programs we've written so far aren't large enough to require functions so it sounds like there might be longer and more complex programs this week!

Lecture Materials
Just the two lectures this week.  22 minutes worth.  That's the shortest amount of videos so far. 
1. Using Functions video (09:56)
2. Building Functions video (12:19)
Dr Chuck explains that functions are bits of reusable code, it's a section of code that is stored and then repeated in different parts of a program to save you repeating it.  D.R.Y - don't repeat yourself!  It makes it easier for debugging, if there's an error in that repeated code, you only have to sort it out in once place, rather than all the places you copied it throughout the program.  A lot of the things we've been using so far such as print and float are functions that are already built into Python. 
The second video explains in more detail how the functions work and how we can build our own functions should we need to in the future.  It was quite tricky, there was a LOT to understand in there.  Dr Chuck did seem to be repeating things in order to help with the understanding, and it did get a little monotonous.  On the other hand, it kind of could have done to be a bit longer because I wasn't entirely sure I was getting it all.  I understand how the functions work and recalling them, I think the problem is with arguments and parameters, they seem to be the same thing but with different names and I find it confusing.

Assignment
The instructions for the assignment say: Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of pay in a function called computepay() and use the function to do the computation. The function should return a value. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use input to read a string and float() to convert the string to a number. 
So this is the same assignment that we did in week 4, but creating a function to do the calculation instead of having it in the main body of the program.  It's worded rather badly, I don't quite understand what it means by 'the logic to do the computation'.  I think it means the bit that does the calculation to work out what the pay should be. I really struggled with this, trying to work out which bit of the code should go in the function definition. I tried putting the part that worked out the different rates for the hours over 40 but that just gave me errors. I was tired and frustrated so I ended up leaving it for the day and coming back to it another day. When I came back to it, I realised there was some code already supplied for us to work with which was:

def computepay(h,r):
return 42.37

hrs = input("Enter Hours:")
p = computepay(10,20)
print("Pay",p)

I tried running that code in PythonAnywhere to see what it did. The second line which says 'return 42.37' means that whatever hours and rate you provide, it will always give the pay as 42.37. But that line is where the calculation needs to be. I went back to the code I'd done for week 4 which was:

hrs = input("Enter Hours:")
rate = input("Enter Rate:")
h = float(hrs)
r = float(rate)
if h >= 40:
normal = 40.0
over = h % 40
pay = normal * r + over * (r * 1.5)
print(pay)

The calculation here is in line 8: pay = normal * r + over * (r * 1.5)
so that should replace the return 42.37 in the section which defines the function. Then the calculation needs to be recalled at the bottom, where I originally had it. It sounds very confusing, but this was the code I came up with:

def computepay():
return normal * r + over * (r * 1.5)

hrs = input("Enter Hours:")
rate = input("Enter Rate:")
h = float(hrs)
r = float(rate)
if h >= 40:
normal = 40.0
over = h % 40
p = computepay()
print("Pay",p)

You can see there where I've just taken out the calculation, put it at the top where the define function part is and then recalled the function at the bottom. I tried running it and it worked! I got the correct figure returned. Even though it worked, I wasn't sure if my code fitted the criteria for the assignment but I tried submitting it in the autograder anyway. I got the response that my Grade had been updated on the server, so it was right! That was such a relief.

Bonus Interview
This week it's with Nii Quaynor about bringing the internet to Africa.  It was more about the process of setting up committees and organisations, rather than the technological infrastructure so it was rather dull.  Not at all what I expected.

So that's the penultimate week all done.  I can't believe I'm up to the last week now.  Week 6 is Loops and Iteration.

online learnings

Previous post Next post
Up