[Tutor] Wordscramble.py
Lie Ryan
lie.1296 at gmail.com
Wed Sep 16 12:55:54 CEST 2009
David wrote:
>
> kreglet wrote:
>> This is my first Python project. I am doing this to help me learn the
>> language. I was wondering if someone could give
>> me some advice as I have a lot of questions.
>>
>> 1. I would like to have someone look over the code and tell me how
>> to improve it. I am sure that a lot of it can be done better than the
>> way that I have it written.
>> Is anyone that would be willing to do this?
In no specific order:
1.
A file is its own iterator, instead of this:
for line in f.readlines():
line=line.strip("\r\n")
wordlist.append(line)
you can have:
for line in f:
line=line.strip("\r\n")
wordlist.append(line)
2. You are mixing spaces and tabs! PEP 8 (Style guidelines) prefers the
use of 4-spaces. Among other things, it also says to use TitleCase for
class names.
3. The star-import is discouraged for a good reason.
from scrmod import *
import only what you needed
from scrmod import this, that
or import the module
import scrmod
4. instead of x=random.randint(0, c-1), you can use
x=random.randrange(0, c); btw, do you know about random.shuffle(), random.?
>> 2. Although it works as is I would like some advice on improving
>> it. ie. Making it look better, play better, etc.
I happen to come accross words such as these:
DéTENTE
which, I guess, might cause problems with the gameplay?
>>
>> 3. I switched from Windows to Linux about 2 months ago. I read that
>> Python is a cross platform language. What would need to be done
>> to make this program run in windows too?
Usually there is nothing special you need to do. If your program only
interacts with the stdin/stdout/stderr and/or a cross-platform window
manager (Tkinter, gtk, qt) and/or the standard library; then you can
almost be sure that your program will run cross-platform without change.
A certain care need to be taken for path handling; make sure to use
os.path instead of trying to manipulate paths yourself. Naturally, if
you used third-party modules, you must make sure that they support all
your target platforms. Creating self-installer is a bit tricky.
>> 4. Once it is finished, I would like to make it installable for
>> people other than programmers. How?
>> These are a few of the things that I would like to accomplish.
>>
>> The code can be found at pastebin.com:
>>
>> http://pastebin.com/m1ab9a734 Wordscramble.py
>> http://pastebin.com/d2686ec4 scrmod.py
>> http://pastebin.com/m4f611e0c wordlist.py
>
> I got it to work on linux but first had to use dos2unix to remove the
> ^M. I am new to programing myself so can not be of much help but to make
> it a python program I have used distutils;
> http://wiki.python.org/moin/Distutils/Tutorial
> Now to get it cross platform I have no idea.
> -david
>
More information about the Tutor
mailing list