This is disappointing…

So I decided to learn python.  I was happy, if occasionally frustrated, with how things were going.  When I got onto the idea of writing my own code (in the form of a text ‘choose your own adventure’ game) I was really getting into learning.  I got this idea in my head to port this game into Android and eventually develop it into more and more sophisticated levels (i.e. add some images, sound, maybe even a video game down the road….wayyyy down the road) but after much research, and in particular the following video, I found that doing so is not terribly practical at the moment.

I wonder if it would be worthwhile pushing for a mobile OS that does use Python as a main programming language (like Obj C for IOS and Java for Android).  It would be like a 3rd entity coming into the split market (as the current 3rd entity (Blackberry) is on its way out).  Sadly, this type of idea is well above my understanding on how such things would come about, not to mention the enormous amount of effort it would take to develop such an OS.  But hey, the idea is here now.  Feel free to pass it around and who knows…maybe someone would give it a shot.

Getting back to Python….finally…

Well, now that I am able to finally breathe, somewhat, I am getting back to developing my Choose Your Own Adventure text game.  Check out the latest minor tweaks to the game here.  Note: The Sci-Fi and Horror text is not working, merely a placeholder for their eventual development.

If anyone is interested in writing out a simple Sci-Fi or Horror storyline, similar in style to the Fantasy one, let me know.  Nothing complicated, just a bit of crazy fun.

Still not getting it…

What I love about the Internet is the community that happens at times.  I posted my game code up for all to see and review and I received a lot of good feedback.  One guy, Bob, event went out of his way to help me understand classes and wrote up a basic structure for my style of game.

from collections import OrderedDict # for storing menus.

# adjust for Python 2 or 3
import sys
if sys.version[0] >= ‘3’:
getUserInput = input
else:
getUserInput = raw_input

def main():
place = ‘road’ # starting place
while place:
place = places[place].enter()
if not place:
ans = getUserInput(‘enter yes if you want to play another round.’)
if ans == ‘yes’:
place = ‘road’

class Choice:
def __init__(self, prompt, next, description=None):
self.prompt = prompt
self.key = prompt.partition(‘ ‘)[0].lower() # get 1st word of prompt
self.description = description
self.next = next

class Place:
defaultChoice = Choice(”, ‘cycle’, “I don’t understand that!”)
def __init__(self, name, description):
self.name = name
self.description = description
self.choices = OrderedDict()
self.menu = ”

def addChoice(self, choice):
key = choice.key
self.choices[key] = choice
self.menu += choice.prompt + ‘\n’

def enter(self):
print(self.description)
while True:
userChoice = getUserInput(self.menu[:-1]).l

ower()
choice = self.choices.get(userChoice, self.defaultChoice)
if choice.description:
print(choice.description)
if choice.next != ‘cycle’:
return choice.next# create the kingdom
places = {}place = Place(‘road’, ‘You are standing on a road. Nearby is a small house’)
place.addChoice(Choice(‘Enter the house’, ‘house’))
place.addChoice(Choice(‘South’, ‘road2’))
places[place.name] = placeplace = Place(‘road2’, ‘You are standing on a road, surrounded by howling wolves.’)
place.addChoice(Choice(‘Run for your life’, ”, ‘outrun a wolf? ha!’))
places[place.name] = placeplace = Place(‘house’, ‘You are in a small house. There are keys here. A stairway ascends.’)
place.addChoice(Choice(‘Climb the stairs’, ”, ‘your foot breaks a weak riser and you fall to your death’))
place.addChoice(Choice(‘Exit’, ‘road’, ‘you leave the house’))
places[place.name] = place

main() # start the game

I tried running it and get an immediate error.
ImportError: cannot import name OrderedDict

Well, let me do some research into this and see if it’s just a Python version issue because in the code there is something about checking version and using different routines.

Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48)
[GCC 4.4.5] on linux2

Ah ha, the Docs on OrderedDict say new for version 2.7 whereas I seem to have version 2.6.  How odd though, I thought I was running 2.7 all this time.  Off to Google to find a link/how-to on this.

I found this link, which does include an automated script to do the update for you (this one compiles from source) but experience has shown that such things never turn out exact and I really can’t be bothered to spend hours and hours trying to figure out what went wrong.

Then I found this link, which suggests using a mix of Debian versions, which I know causes bad things to happen.  Besides, Debian Wheezy is now stable and as soon as I get an external HD, I’ll be backing everything up and upgrading my OS, which includes v2.7 of Python.  So, time to wait then I can try this code out.

I will say this though.  From what I read in the above code, I’m not convinced that it will be any easier to program.  It just doesn’t look/feel intuitive enough when trying to debug an error message that might come up.  That and it seems to scream ‘complication’ when the following is to be adhered to.

      The Zen of Python

    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!

Side Note: Why the hell is WordPress suggesting “Pink(singer)” for this post?!?! Also, they suggest ‘transportation’ for just about every, single post I have written for any of my blogs here after the publish phase.  What gives WordPress??  You desperate for some Transportation posts??

OOP stuff. Worth it?

As I’ve been on this quest to understand the Class function in Python, which is a form of Object Orientated Programming, I am slowly finding some resistance to the ‘must use OOP!  All the time!” type of mentality.  So, being the type willing to consider many ideas before making up my own mind, I’ll be reading more on this stuff.  For now, here is some food for thought.  My quick cursory of the article seems to mean OOP is just another tool that may or may not be the best for a given coding scenario.

By , 20 Apr 2013

Introduction

The last decade has seen object oriented programming (OOP) dominate the programming world. While there is no doubt that there are benefits of OOP, some programmers question whether OOP has been overrated and ponder whether alternate styles of coding are worth pursuing. To even suggest that OOP has in some way failed to produce the quality software we all desire could in some instances cost a programmer his job, so why even ask the question?

Code Academy bugs

I finally realized today that I _can_ just continue with the exercises by merely selecting the next one from the drop-down choice and not just be stuck waiting to find out why my code is supposedly ‘wrong’.  I also found out that there is a bug in that particular exercise.  Two days and suddenly realizing that my code is just fine.  Talk about screwing up new people to learning code.

So, taking this new-found knowledge with me as I go, I find this little gem of a bug.

 

So yup, moving right along.

If you are new to learning programming, you need to understand that you will be using someone else’s programming to learn programming, and it is rarely perfect.  Document the error and move on.

For those who downloaded the game code..

Seems I made a rather dumb mistake and uploaded a version of the core code that was a partial conversion for the next update, and thus failed miserably when you ran it.  I am mobile but did a quick edit and updated the link with something that at least works and doesn’t throw errors immediately.

Thanks for your patience and efforts!

 

Started a new course…and LPTHW update

Greetings all,

OK, so first the update.

While I was working on Lesson 43, and trying to understand Classes (this seems to be a hard subject to just explain, let alone follow good examples, which are even harder to find).  I decided to hop on over to the #python channel in IRC and get some feedback.  It was pretty unanimous that the example used in the lesson was silly.  To be honest, I was thinking so as well.  See, I created that ‘choose your own adventure‘ code and when I saw LPTHW’s example, they basically just moved the functions in to classes.  Well I thought that was kinda dumb because you aren’t really saving anything.  Maybe you can move/call classes around a little easier but otherwise, it’s no different from my example without.

This saddened me.  Here I am, at yet another course that dead-ends when it gets to Classes.  The gang on #python did direct me to some cool resources (See the Python Resources page here) and I did watch a video on Classes but it got real deep real quick by one of the guys who develop Python itself.  So that was of no help.

So, I have an idea for a new version of the ‘choose your own adventure’ game.  I’ll share it when it’s closer to being done :)

 

New Pages

I’ve added a new page today(the other one was added last week in case you missed it).

Python Resources : Here I plan to continue adding resources as I find them.  I’ll do my best to make sure they are up to date and still around.  If you know of any others that aren’t listed, please feel free to let me know so I can add them.

Learning Python Reviews: This page is a mini-survey to help people evaluate the quality of courses offered for first time Python learners.  The total results of the survey are posted at the end of the survey but I’ll try to get them online on the Resource page as well to make use of them pro-actively.  For now, please consider taking the survey so the information can be used for the betterment of all.

 

(Updated 31/5/2013) So, I’ve been following the LPTHW lessons…

I am finally finishing off the Learn Python The Hard Way lessons that got shelved because the Coursera course was taking up all my time.  One of the lessons included making a program that is basically a ‘choose your own adventure‘.  It’s a pretty basic program, mostly a lot of ‘if’ statements to choose which section happens next.  While not terribly complicated and barely any different from many of the original books that did the same, it was a good lesson in tracking how your program flowed and making sure the results were what was expected.  So, here it is.  Yeah, I borrowed heavily from a Dungeons & Dragons module but I’m not selling this, nor do I ever plan on making money.  It was merely backdrop material, so Wizards, don’t bother suing me :P

Since pasting code here and trying to keep its format doesn’t work so well, you can see it here:

http://bpaste.net/show/9ScYudSvDyHuUQdWDR13/

BTW, it’s not exactly SFW…so keep that in mind.

Some useful tips from python-forum.com

I like to have as many resources available when I need help.  Who doesn’t?  So, besides Google (which needs a certain skill to get the right answer to your specific problems typically), I also make great use from IRC.  These are the channels I use on freenode.net (YOu can get to them either by http://webchat.freenode.net or download a client like XChat)

#python (You need to have a registered nickname as well ask for a ‘cloak’ in the #freenode channel, otherwise you don’t get in.  It’s all free, but just need to ask.)

#python-apac (This is a new one for me as of today.  It says it’s for the Asia Pacific crowd but there’s only 2 people in there right now and I’m one of them.  Was hoping to find some english speaking Python guys in my own timezone to chat with.)

#python-forum (This is the official channel for python-forum.com, not a lot of people there but also not many who are currently active for my time of the day too.  I suspect it might be a mostly ‘western’ timezone active channel)

#dragondon (This is my own personal channel.  A few geeks hang out there.)

I just registered on python-forum.org.  I wanted a place that I could post up questions.  Seems a cool place so far.  They have some cool tips and I thought I would share one of them that I found.

****

Avoiding massive elif statements
the first example shows elif statements as you would learn them in any tutorial, the second however shows the same thing done but with no elif statements. It does the same thing but reduces the code and makes it more debugable in the future.

choice = input(‘enter a number’)
if choice == ‘0’:
print(‘you chose zero’)
elif choice == ‘1’:
print(‘you chose one’)
elif choice == ‘2’:
print(‘you chose two’)
elif choice == ‘3’:
print(‘you chose three’)
else:
print(‘out of range/invalid’)

user_choice = {‘0′:’zero’,’1′:’one’,’2′:’two’,’3′:’three’}
choice = input(‘enter a number’)
try:
print(‘you chose {}’.format(user_choice[choice]))
except KeyError:
print(‘out of range/invalid’)