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"
        break
    else:
        print "You did'nt find the key in the " + items[choice  -1]

print "\nThankyou for using text adventure.py!"

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: ")
        try:
            n = int(n)
            break
        except:
            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!

thanks for your feedback, i will use those things you suggested :)

Member Avatar for leegeorg07

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
"""

(http://www.python.org/dev/peps/pep-0257/)

Consider validating user input. What happens to this

items[choice  -1]

when the user enters something out of the expected range?

whats so bad about using
keyLocation = random.randrange(1, 5)
??

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.

len(items)

ok thanks! :)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.