Hello and welcome to yet another Worked
Exercise from Python for Informatics Exploring Information.
My name is Charles Severance and I am your exercise coach.
I'm your personal trainer, your personal assistant.
Welcome to the software development gym.
This material, of course, is copyright Creative
Commons as is the textbook as well.
So just make sure when you're using these exercises that you're using them properly,
particularly if you're taking this class and you have an
assignment that includes this exercise, you probably should try it first.
I mean, these exercises are there so that you learn the material and if you just
sort of, take the crutch every time, then you
won't learn the, to do things on your own.
So enough of that lecturing.
Other details you can get from pythonlearn.com.
So today we're contemplating exercise 6.9 and our goal is
to write some code, to parse lines that start with this, something
blah blah blah blah, colon, and then we're going to chop off
the second half and convert it to a floating point number.
In a later chapter we'll do this in a loop, but for now we're
just going to like hack one line and just see what, what it looks like, okay?
And so, so this one's kind of a silly one but
we'll do it, we'll do it anyway.
So this one, let's grab this string while we're at it and start up TextWrangler.
Oh, the last exercise is still there.
Start TextWrangler and start the Terminal program.
In my Terminal program, I'll go to my desktop,
I'll go to Python for informatics folder,
and I'll paste that string in and I'm just going to call
my string x for now and put this in a string.
Later, you'll be reading this from a file but
this exercise is not to read anything from a file.
This exercise is just to parse that string.
So it's going to be pretty simple.
I will print x just for yucks.
We could make it so it would
prompt for this string in a
loop or something like that but we'll, we'll leave that for now.
We're just going to, our job is just hacking and slashing.
So the first thing we're going to do is
we're going to use the find function in the string library.
We've got to make a variable called position and we're going to say x dot find colon.
It's like, where is that colon at? Print position.
And we're going to save this guy as spamchop.py
And, of course, the colors change and we do an ls and here's spamchop.py
So it's python spamchop.py, and again there's no input here.
Try that again, spamchop.py.
So there we go.
That basically says that the position of this letter is in position 18.
So the next thing we're going to do is we're going
to just sort of say, okay, let's chop off the first part.
print x and we're going to use string slicing, p-o-s colon,
which says start at position and move to the end.
So I'll save that one and I'll run it and that looks pretty close.
We got rid of most of it but we still don't, we still have the colon.
So we gotta chop off one after the position of the colon.
We actually are not interested in the colon.
So we just add one here and then we go, spamchop.
Now, we're going to see if the float cares about this.
I don't think so but we'll find out. num equals float
and I'm just going to say x of position plus 1
because I can give it an expression here, to the end, and let's see what happens.
print num.
So what do you think is going to happen?
Think it's going to work?
Think it's going to blow up?
Think these spaces at the front are going to freak it out?
Let's find out.
No.
The spaces don't freak it out.
So we now have a number and if I did, if I did a print of type,
which is to be sort of like obsessively compulsive,
you couldd say num comma, type of num, and so
proven to me, and I can, I can comment these guys out.
We don't need those guys anymore.
And that is a floating point number and it's all parsed and it's cool.
I mean it's pretty simple, right?
You go find where this thing is, advance
one character beyond it, and then slice it out.
Simple enough.
Thanks for your time.