Skip main navigation

New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £29.99 £19.99. New subscribers only. T&Cs apply

Find out more

Worked Exercise: 2.3

Worked Exercise
[Instructor] Hello, and welcome to Python for Everybody. My name is Charles Severence. In this short video I will be explaining how to run Exercise 2.3 where we prompt for some Hours, prompt for some Rate, and multiply them together and print them out with a little paid message. So this is 2.3. Some of you will immediately want to go to the Autograder and do your homework on the Autograder. I really would rather you didn’t do that unless of course you’re doing this on an iPad or an Android or something where you can’t install Python. But you have to realize that the Autograder isn’t forever.
You can only go so far with the Autograder and eventually you have to write a real Python program. So I’ll eventually show you how to run this in Autograder but I’m gonna instead show you how to run it in the terminal. So I’m first gonna go into my Python for Everybody folder and I’m gonna make a new folder. Command+shift+N is what I just did there, ex_02_03 for Exercise 3.
And so there’s Exercise 3. And I’m also gonna go into Atom which is my text editor and see, I have that folder. So I’m gonna make a new file and I will say print, I’ll just say (“PY4E”) and then I’ll say File, Save As, and I want to make sure it’s in here, and it’s gonna be ex_02.
I don’t like putting spaces in file names. Some operating systems can handle them but that’s why I’m using underscores here. So I would avoid using spaces in file names. So as soon as I give that a (“PY4E”), as soon as I give it a Python suffix, I’m there. And so it shows up there in my desktop. And now I’m gonna run the terminal program so that I can get there. So, cd Desktop, cd Python for Everybody, that’s the folder on my desktop, and if I even do an ls, I see a couple folders and a file. We can say ls minus l and see a little more detail that these two are folders and this one’s a file.
So, change directory, cd ex_02_03.
And so now I’m in that folder and if I do an ls minus l, I see that file. I can also do an ls without the minus l and see the file. And now I say, python3,
and it runs. You’ll see me, no matter how many times you watch me, you’ll see the first thing that I do is get to the point where I know I’m in the right directory and I can run a little hello program before I start coding. I just don’t like being crazy, right? So now I’m gonna go back and take a look at my assignment. Enter Hours, you gotta prompt for hours, ask for a number. Enter Rate, prompt for rate, and then calculate pay. So there’s a couple of input statements here. xh is my variable I’m gonna choose. Later I’ll choose more effective variables but for now I’m gonna make them silly. Enter Hours colon space
and then I’m gonna copy and paste and call this xr for Rate.
When you’re doing this, you need to be very careful. And so now I’m gonna calculate xp
which is xh times xr,
and then I’m gonna say print (“Pay:”,)
I don’t need to put a space because this comma effectively creates a space, xp. And then I’m gonna save that. I’m gonna switch to my terminal program, clear my screen in my terminal program, and I’m gonna type up arrow, because I already typed python3 so my Hours, I’ll just start with something really simple that I can calculate in my head, 10 and five. Whoops. “can’t multiply sequence of non-int of type ‘str’”.
Here we have a traceback and again, I encourage you to realize that these tracebacks are not personal attacks by Python on you even though they might be frustrating. (sighs) So the way to parse this is start by saying line 3, something’s wrong at line 3. It’s pretty good at knowing what line it is or it’s either that line or the line above it, and it’s something about multiplying. What it’s really saying is, “I’m confused. “I have to stop “because I cannot understand your instructions.” So the problem here, of course, is that this is of type string and so you can’t multiply a string times a string, okay? So we can convert this using a float,
so that’s a function call now. We’re passing the string xh in and the value we get back is the floating point version of that and then we call float for this as well. Now I’ll save that, always remember to save.
So I’m gonna run it. I’m gonna run my Hours of 10 and my Rate of 10, and it’s 100, and so that looks pretty good. So let’s go ahead and try to run this in the Autograder, and this is my idea, is you’ll take this, and you’ll copy it, and you’ll go back to the Autograder now and just paste this in. And so it says, “Use 35 hours and a rate of 2.75.” So let’s check the code. 35 hours, OK. 75.
Oh no, 2.75. Two point seven-five. And so it’s running, and it’s running, and it’s running, and it works! And, of course, now I’ve got my Grade. So this idea where you work here to get your assignment done correctly and then you run it in the Autograder is the way I intend for you to do it, but again, if you can’t do it that way, it’s a great way to get started to just write your code in the Autograder. And, you know…
You can change your code in the Autograder and then run it again. Of course, this is gonna fail. 35. And 2.75.
And of course you get a Mismatch, and now it’s angry at you. The Mismatch here of course is because I print Howdy Pay in Pay, and it’s real picky about it, and you think, “Oh, I got the 96.25 right.” Well, it doesn’t really care so much about that. So let me go ahead and fix this and run it so we leave on a successful note! 35 hours
and 2.75 as the rate per hour. That’s kind of a low rate per hour. And we’re getting successful and of course that means that you now have a grade on Assignment 2.3. Look at that, I got a grade on Assignment 2.3. Unless, of course, you’re running this in some other environment, okay? Thank you so much and I hope that this has been useful to you.
This article is from the free online

Programming for Everybody (Getting Started with Python)

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now