As before, Week 4 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 3 of the textbook and pdfs for the lecture slides. The focus this week is moving from sequential code that runs one line after another, to conditional code, where some lines are run and some are skipped, depending on various conditions.
Lecture Materials
There's just two lectures this week. 32 minutes worth.
1. Conditional Statements video (14:20)
2. More Conditional Statements video (17:39)
The first video introduces if statements which is where the program chooses between several options, e.g. if such a thing is true, do this thing. Then there's if and else together which also has options, e.g. if such a thing is true, do this thing, else do this other thing.
It also talked about indenting which apparently is v important in Python with conditional code. Most other languages use indenting to make it easier to read but it doesn't have any impact on how it runs, but Python needs the indenting to execute code properly. And it has to be 4 spaces, not tab!! Using tab will make Python complain at you.
The second video brings in more complex conditional statements, starting with elif, which is a combination of else and if. This is used when there's a list of options and the program runs through each one but as soon as one is True, it skips the rest. Finally there's the try and except structure. This is used when there's a bit of code that might fail, so it's saying to Python to give the code a try and if it works, that's great, but if not, to do this other thing. There's a lot to get my head around there. I'm familiar with if statements and else is vaguely familiar too, but the rest is new.
Assignment
The first assignment says: write a program to prompt the user for hours and rate per hour to compute gross pay. Pay the hourly rate for the hours up to 40 and 1.5 times the hourly rate for all hours worked above 40 hours. 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 once again we're being naughty and using float for money! Tut tut! This assignment looks quite tricky because first I need the program to take 40 hours away from the inputted amount of 45. Then it needs two calculations - one for 40 hours at 10.50 and then one for the remaining 5 hours at 1.5 times 10.50. Oh! Remainder! There's a symbol for that, it was mentioned in last week's lecture. So first the program needs to check if the inputted hours is over 40, then to store the 40 as normal hours and the remainder as overtime. Then I need to multiply the 40 normal hours by 10.50, but the overtime hours as 10.50 times 1.5. Obviously the base of the code will be the same as for last week's assignment so I can amend that to add in the if statement, the different options, and the amended calculation. So the code I've come up with is:
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)
I used PythonAnywhere to run it this time, which had a slightly different process to the Python Playground. Instead of typing the code directly in and pressing run, I had to make a new file, put the code in that, and then run the file in the bash console. It's a much longer process, but it's similar to the way it would be done if I had the actual Python software installed. So it seemed like a good thing to get used to doing, rather than using the Python Playground. I worked out the code first (typing it into Evernote which has a handy code block feature), then saved it as a new file and ran it in the console. I had to tweak it a little because I'd told it to take 40 from the 45 hours, which meant that it was storing 5 twice, instead of storing 40 and then 5. I also misread and didn't realise that I had to ask the user for the hourly rate as well. Once I'd made the changes, I got the result of 498.75 which was correct. Yay! I copied and pasted my code into the autograder and got the message that my grade had been updated. Phew!
After this is a video for the next exercise in the textbook. That confused me - and several others on the course - as I'm used to going through the steps in the course, not a textbook. This exercise was to redo the same program as before but including the try and except function to give an error when a user puts in text rather than figures. Most of the video was taken up with Dr Chuck getting confused because he was in the wrong folder, then he got sidetracked talking about command line commands, and finally how he liked to double and triple check that he was editing the right file. Given that he'd started off in the wrong one, I can understand his need to check! It was just a lot of stuff that wasn't entirely relevant and didn't give a brilliant impression. The final 3 minutes of the video actually dealt with the exercise in question *headdesk* I had a small problem with it because after doing the error checking part, I didn't know how to stop the rest of the program from running because Dr Chuck hadn't told us that yet! That was rather annoying.
The next assignment says: write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error. If the score is between 0.0 and 1.0, print a grade using the supplied table. If the user enters a value out of range, print a suitable error message and exit. For the test, enter a score of 0.85.
Well, the table has a list of grades from A to F, and that looks similar to the example code used to explain how the elif function works so I imagine that is the best thing to use here. I copied the code from the example, changing the figures to match the table supplied for this assignment. I added an error message for any score that was over 1.0 as well as the error code from the previous exercise for text entries. My completed code was:
score = input("Enter Score: ")
try:
fscore = float(score)
except:
print("Error, please enter numeric input")
quit()
if fscore > 1.0:
print("Error, please enter a score between 0.0 and 1.0")
quit()
if fscore >= 0.9:
print("A")
elif fscore >= 0.8:
print("B")
elif fscore >= 0.7:
print("C")
elif fscore >= 0.6:
print("D")
elif fscore < 0.6:
print("F")
Look at that! 21 lines of code with two error messages in it! I gave it a thorough testing in PythonAnywhere, entering 'one' to get the error for a numeric input, and '2' to get the error about a score between 0.0 and 1.0. Then I entered various numbers to get all the grades. It all seemed to work perfectly, so I entered it into the autograder and held my breath. It said 'Grade updated on server' so it was all correct and I got my 100 grade! YAY! 😄
Bonus Interview
Just one this week, and it's with Massimo Banzi about the Arduino. I remembered that Arduino was mentioned in the interview about the Raspberry Pi but that was as much as I knew about it. It's an open-source electronic prototyping platform that enables users to create interactive electronic objects. It's a bit of a boring video, just talking about how Arduino started out. I did like how he said:
"there is a trend that I am not particularly happy with where people just buy devices and they just use the devices. But they are not aware of how you program, how you design, you build things with the device... So clearly, if you know how to design and build things, you can affect the world that surrounds you. If you are not able to participate in the world of creation in the digital space, you’re left out. Somebody else is gonna design your world... if there’s no innovation, if there’s no sort of renovation in a way inside the marketplace, then one company decides that that’s the way you do a certain thing, and that becomes the only answer to a certain question. And nobody stops to debate that."
That is such a good point, especially with regards to children. It's important that they become creators, rather than just consumers. And the idea of one company having a monopoly over any aspect of technology is definitely a huge issue.
So that's week 4 done. Week 5 is about Functions.