In this exercise, we play with splitting, adding to lists, & the join function, referred to in this stack overflow post as the inverse of the split function.
split cuts up any old text string, like mary had a little lamb and turns it into a formal list, like ["mary", "had", "a", "little", "lamb"], with each word made into an indexable (numerically anyway) item in a list, via the format of stringofwords.split(" "). I’m not toooootally square on the quotes in the formatting of the use of split, but I can punch it in well enough til I grok it fully.
join depends on the same structure of listofitems.join(" ") to de-string-ify it. Handy!
There’s a bit also with, hmm, how do you call it, positional list comprehension, maybe? For example in the previous given list, the ["mary", "had", "a", "little"} et cetera, position 0 is the very first one, mary, where position -1 is the very last, little.
This was a good one! I think the exercises will stay challenging through the next thirteen : )
Lessons 32, 33, and 34 were list comprehension and conditionals like if/elif/else, for, and while, all of which I feel fairly comfortable with. Exercise 35 was an application of these ideas in the form of, woohoo, a text-based adventure game! This one came in a bit longer at 76 lines, which I honestly love copying in. He had a few handy tricks that he hadn’t talked about, like the exit(0) which kicks you out of the program, and a few clever nestings and conditionals, like the combination of the boolean in one of the “rooms” with a while and an if/else.
Rather than give each of those its own post, since I don’t have much to add having learned those functionalities long ago, I moved over them so I could advance a bit more quickly. In exercise 36, he asks for a unique game! So I made one! Woo-hoo! After a not too long trial and error period, I came up with the following game, of which the following is probably the most representative output that I got when I had someone else play it. Check the repo if you’re interested in the code! the output is more fun anyway : )
WELCOME TO THE DUNDJEL
you can go north and south. maybe other directions?
> north
You carry on your dumb way north, dummy
You went north! That's probably fine.
You see an A and a B, and also a button.
> a
yeah, you only have so many options, here.
nice that you think I'm smart enough to come
up with more stuff, but I haven't.
> b
yeah, you only have so many options, here.
nice that you think I'm smart enough to come
up with more stuff, but I haven't.
> A
consider all of your options my friend.
but not today, for today you die.
you lose. you see a howling hag:
SHE'S A WIIIIIITCH
BOOOOO. BOOOOOOOO!!
It makes me laugh which is really all that I’m looking for in my own text-based adventures. There IS a way to win, but it doesn’t look like they made it!
Hey, quick sidenote, have you seen Depression Quest? It’s eligible for Steam Greenlight, and it, well, my description won’t do it justice, the concept is amazing & you should just go take a look.
Hey-o, conditional statements! I breezed through a bunch of lessons because we’re getting back to the kind of programming that I have already learned quite well. For the novice, my blog will not be as helpful as it may have been for previous lessons. The issue is that I am super ready to finish LPTHW!
For Exercise 29, he introduces conditional statements, the results of which I could see as I typed them in.
people = 20
cats = 30
if people < cats:
print "Too many cats! The world is doomed!"
if people > cats:
print "Not many cats! The world is saved!"
Basic less-than/greater-than operators – clearly the world is doomed! That’s about all there is to this exercise. (Ed: T.I.L. that less than and greater than symbols have to be inserted into with & l t ; and & g t ;, respectively! Awesome!)
In Exercise 30, it’s the same thing but introducing, hmm, subconditional statements you might call them? Rather than just if and else there’s also elif which takes its place before the final else in which you can make a further conditional statement within the if statement. This has been one of the handiest things I have learned (though I learned it long before this lesson) in Python, and it has analogues in every other programming language as well. Very cool.
In Exercise 31, the conditional statements get wrapped up in a “game” in which your player gets to choose from various options, to either be driven mad by Cthulhu or ripped apart by a bear. HMMM, which would you choose?? The purpose of this lesson is to start getting the student comfortable with nested if-statements. Old hat for me – I need to move a little faster!
With that, I think I need to go through these lessons a little more quickly than I want to blog about. I will write another blog when I come up against something that is truly new to me. I know Zed Shaw has challenges for me yet!
Hoo-woo-woo-wow, Lesson 28 was a zipper, but I imagine that is only because I took Discrete Mathematics about a year ago, in which boolean logic was beaten into us : )
I thought this truth determination was especially funny to find. Can you find the correct truth value? "chunky" == "bacon" and not (3 == 4 or 3 == 3)
I got them all right! Moving on, breakneck pace my friends! But really, I want to get back, so badly, to “copy this in & solve a problem.” I looked ahead a bit & I think the next one is like that. Oooooh I hope!
In trying to set up a proper website for myself, which I won’t link til it’s at least somewhat how I want it to look, I have been a little frustrated, so as a side project, I am trying to make myself an eventual budgeting program, based on my brother’s Brinance, and by based on, I don’t mean based on at all – I am going to write my own, ha ha! and then see how his differs from mine and in the process learn how to read a bit of perl. Exciting!
I want to try to bang out the rest of LPTHW over my winter break, which has just begun. We shall see if that is doable! Then I want to move on to Learn Ruby The Hard Way, as Ruby is super-sexy right now.
AHH.ย Feels good to be back to ordinary python (though I now have much more planned for heroku/django!).ย Today I wanted to punch out a quick lesson since it’s been a few weeks for a Learn Python the Hard Way, and I want to maybe finish the book by the end of the year.ย Here’s exercise 26, a debugging exercise. Excellent. *cracks fingers* Ok! First, I copied in the required “code” from here into nano & immediately set to correcting all the tiny errors, like missing parentheses, spelling errors (not that those matter too much in this exercise but pedantic habits die hard), and inconsistencies with function naming/calling. I knocked this out in about fifteen minutes. Exciting!! I should do another, even though it’s thanksgiving eve & that bathroom isn’t going to clean itself ๐ ok but yeah just one more for real, this didn’t quite scratch the itch! and then I’ll get to the bathroom, jeez DAD LAY OFF
Edit: OH MY GOD THE NEXT EXERCISE IS TRUTH TABLES! HOORAY!!
Heroku Haiku
Are not easy to write, no :
You can tell by lame.
Ok, that might be a bit of a stretch, but I think it’s an appropriately silly start to my Heroku knowledge! Today, I will learn to use Heroku, a platform-as-a-service (PaaS) cloud application that, as far as I can tell so far, acts as a sort of auxiliary server for development.
I am going to start on the quickstart and then moved on to the headier document entitled How Heroku Works. I’ll be documenting my process in this entry, so, stay tuned! (I’m talking to YOU, happy-hour-free loner out there!)
Update 1:
Ok, first update, I’m finally putting git onto my machine. It was (or at least seemed) awkward on windows, since the command line is just a whole other beast over there, so now I’ve got it on the linux end. fabulous; all I did was type git into bash & it said “if you want to download this just type sudo apt-get git” and that is so amazingly helpfully easy. I think this will be a whole new reason to use github, which I haven’t been doing for a while because I’ve been so into the CLI lately and I only ever used github’s browser interface. Progress, of a type!
And now I’m trying to flash-dash convert a python program I wrote in 3.3 to 2.7! raw_input is weird! ok, a half hour later & I’ve got that locked down, the differences between which you can see at its github repository.
Update 2:
And now I’ve got the heroku toolbelt for linux on the machine, here. Why why why does it seem so much more approachable on linux? Why is the scrolling text of installation so very comforting to me? Why, indeed, is the sky blue. I actually did the boneheaded thing of accidentally typing my master pw in with capslock on, really! so that is a funny/dumb thing for me to have to remember. [edit: since fixed] and with my successfully converted python 2.7 program, which I have cloned to my system via the heroku dashboard helpful page, I’m now on the git push heroku master, the entire logistics of which I am as yet unsure.
Ah crum. it went through the git push heroku master, counting objects, compressing them, writing them, and THEN it has said
Push rejected, no Cedar-supported app detected
To git@heroku.com:stormy-scrubland-8935.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@heroku.com:stormy-scrubland-8935.git'
but doing the heroku open still worked just fine. I do not know what cedar nor “stormy-scrubland” mean, though I’m guessing the latter is some kind of version of cedar, or some version of heroku. it seems like that kind of cute hacker convention in line with “llooming llama ubuntu” or whatever the L was called, ha ha! edit: ok, weird. stormy-scrubland-8935 is the name of my app on heroku? somehow? that’s an odd name generator. [edit: I think that this is because I did not create a heroku environment before pushing the git change, with a quickheroku createbeforehand.]
Update 3:
Now I’ll be installing Flask, required on the Getting Started [in heroku] With Python page. It says to have pip, virtualenv, and of course, python installed in advance. I have already installed these items, so that is fortunate. Now, I’ve activated the virtual environment (venv), and while I’m glad to know that I can’t borf (borf is a VERY technical term) my computer irrevocably while within the virtual env, I am Worried that I have already done so, & that with any programming & new set of tools to download, I should have already been putting it all in a virtual environment. BLAST. ok, now I need to find out if my entire environment is salvageable, haha! I have heard about this linux problem, that is to say, when you start a new project but instead end up necessitating a machine-wide wipe and OS reinstall. Oh jeez. Ok!
And now I’ve figured out virtualenv. It was giving me tons of crazy errors, like “nothing in “*”” which means that there was nothing in the indicated path, as that’s what the asterisk means, and also “nothing of *.pyo” and “nothing of *.pyc” which are python compiler file extensions, so I got out of the virtualenv I’d made above & deleted it, and started over using virtualenv venv --distribute --no-site-packages. then it worked! now, whenever I’m screwing around with any programming, I know how to create a virtualenv and I won’t (further) irrevocably borf (still a very technical term) my machine!!
FINAL Update: WOW! What a terrific exercise. Many hiccoughs along the road, but now, at the end, I have learned so so much. For example, now I can tell you what Heroku really does – it’s something that will take in and run your program, but is separate from actual domain/web hosting. Though the tutorial does host it on heroku, it’s obviously intended for your own site, ha ha! Also, I’m now totally comfortable with virtualenv, which is a really crucial step for me. There’s more, but I’ll reserve it for a future update! Thank you for reading this megapost!
EDIT: so. this installation has been Really Hard. finally, having installed wsig, virtualenv, and virtualenvwrapper, I finally installed pip to handle python-related downloads, and while muddling yet again (really – for like the tenth time over the last couple days) through trying to just download django, my friend suggested, “well, why not just sudo pip install django?” HA! beautiful! so that worked, immediately, and now I have django on my linux box. woo-hoo!
now to USE it, to make a django-structured (probably wrong terminology) personal site, to showcase my ~tAlEnTs~! aaaahhhh
Here we’re getting into writing programs to be used, actually used, on the command line, and that is so exciting. It’s been almost seven months since I made something to actually use, and while I have no real need for a sentence sorter, to have written a program that I can then manipulate in the py shell is REALLY REALLY NEAT, to me.
At first, I struggled a bit with some of the definitions, but I really enjoy separating the definitions from the actual what-you-want-it-to-do-ness of the program, as it were ๐
Seeing this: def print_first_word(words):
"""Prints the first word after popping it off."""
word = words.pop(0)
print word
def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1)
print word
was pretty exciting, as I recall the idea of “pop”ping from the approximate third of Douglas Hofstadter’s Gรถdel, Escher, Bach: An Eternal Golden Braid that I read, in one of the interchapter dialogues. Pushing and popping have a very clear analogue in computer science, and while, looking at this particular exercise of Mr Shaw’s, I think that it is not actually the same idea, ha ha! it is still a nice callback even if unintentional and unrelated!
One of the Common Student Questions for this exercise, too, was “what is the difference between print and return, and his answer is somewhat crummy, emphasis mine:
When should I print vs. return in a function?
You need to understand that print is only for printing to the screen and that you can actually do both print and return a value. When you understand this then you'll see that the question is kind of a pointless question. You use print when you want to print. You use return when you want to return.
So I do not really have a clear idea. It seems like once the function has done f(banana) to the stuff, you can either return the result or print it. return seems more verb-y, but the end result seems identical. I will have to read about this!
As a whole this was not a challenging exercise, but it was really fun. I love that Zed Shaw focuses on being able to manipulate python via the structure-level command line rather than via an IDE, like the very popular IDLE which I will not link here. I’ve never imported a function in the shell to play with, and I feel like the possibilities have just expanded. Yeah!
Last week, I completed lesson 24, which was a “make sure you know how to do x, y, and z” exercise of no new material – a fine cap to the no-new-exercises triad of 22, 23, and 24. I am, to be honest, super bored by the idea of not doing new things while going through these lessons and have sped through these fairly quickly.
I am already planning what I’ll do next, after LPTHW. Probably pre-emptive, but there is so much out there and it’s hard not to speculate what I’ll want to do next. I like Zed Shaw’s style and might do the Learn Ruby The Hard Way module, but I might do Learn You A Haskell for Great Good! next because a) Haskell is apparently very sexy right now, and b) it’s probably a good idea to go through a different kind of tutorial than just The Hard Way. Though Ruby might be more career-helpful if I should ever want to make something of this (fairly long-invested at this point!) hobby, so we shall see.