i have written a simple text game and i would like people to comment on my style and comment on things that i could improve on.

# Copyright Tom Tetlaw (c) 2009  #

import random

items = ["pot plant","painting","vase","lampshade","shoe"]
keyLocation = random.randrange(1, 5)

print "You went to bed lastnight felling tired and dizzy."
print "But the next morning, you wake up in a strange room, "
print "definitley not yours, there are some things in the room: "
for entry in items:
    print repr((1 + items.index(entry))) + ") " + entry
print "You notice that the door is locked, so you should"
print " probably find a key."
while True:
    choice = input("Where do you want to look: ")
    if choice == keyLocation:
        print "You open the door and light floods the room"
        print "You did'nt find the key in the " + items[choice  -1]

print "\nThankyou for using text adventure.py!"
7 Years
Discussion Span
Last Post by adam1122

Nothing huge, but

print "You notice that the door is locked, so you should"
print " probably find a key."

should have the space on the end of the first line, just to keep it consistent with the lines above it.

Also, "did'nt" and "definitley" are spelling errors.

Finally, I wouldn't use input() for getting the user's choice. It can be exploited to have python commands run through it (it parses the input[?]). Instead, use raw_input() in a separate function and test for whether an integer had been typed, and if not, loop until True, then return that number. Here's an example:

def getNumber():
    while True:
        n = raw_input("Enter a number: ")
            n = int(n)
            print 'Not a number.'
    return n

The above is what I came up with off the top of my head (untested), but it should point you in the right direction I hope.

One last suggestion is to take full advantage of the object-oriented part of Python. Such as a base class for Items, and classes for Weapons, Armour, Miscellaneous, etc that inherit from the Item class. Also, make an Actor base class, with NPC and Player subclassing it, etc. Good luck!


how do you know where the keyLocation is?

i would have used :

keyLocation = random.choice(items)

this means that you just have to input the word and random chooses the word from the items


Also, this is getting a lot more complicated, but I'm a huge fan of re-usability in code, and I - way back when - created a basis for a text adventure game, using external files for each 'room', organized in sub-directories. They were written up a certain way with special syntax so that they could be read into python and parsed. Making it from scratch was a good experiment, but once I got the system going, I could easily add a new room and it's options/actions and have it tie-in with other rooms very easily.
The room files were just written up in a form I decided on in order to make it usable, but fairly comprehensive, so that my main code was never cluttered with any blocks of 'print' statements, just the more important stuff :P


Here are my few suggestions:

I second the suggestion of this:

keyLocation = random.choice(items)

Your top comment should be:

Copyright Tom Tetlaw (c) 2009


Consider validating user input. What happens to this

items[choice  -1]

when the user enters something out of the expected range?


It's not terrible but if you are going to use it, at least base it on the length of items rather than the 'magic number' of 5.


In retrospect, it's actually the better way because your user input is based on the key index.

However, if you need to get one of the items at random, you should prefer:

keyLocation = random.choice(items)

Also, it's probably wise to make your keyLocation the 0-based key of the items . It may not be a problem in this example, but it could very easily result in a 1-off error when things get more complicated.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.