1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2004
Posts
88
Rep Power
14

#### Need Review

Hey, if you have a second just read this:
I was working on a little code to sort a tree for a gaming tournament. I've bug tested what I've written, and I just wanted to submit it here and see what you guys thought of it. Just to see if I'm practicing good coding habits, not making unnecssary steps, and to hear any suggestions you have.
Here's the code:
Code:
#Created by ** the ** for PDG 2005 Halo 2 Tourney
import random
k = 1
names = [""]
while k < 33:
newname = raw_input("Enter contestant's name: ")
names.append(newname)
k += 1
slots = {}
i = 1
j = 32
while i < 33:
selection = random.randrange(0,j,1)
slots[i] = names[selection]
del names[selection]
i += 1
j -= 1

branch = 1
count = 1
while count < 33:
nessdash = 60 - len(slots[count])
toprint = slots[count]
while len(toprint) < 60:
toprint += "-"
print toprint
if branch == 4:
branch = 0
print "\n "
print " "
count += 1
branch += 1

raw_input("Press any key to exit")
2. Good job.
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2004
Posts
394
Rep Power
55
Hi!

I would do it like this:
Python Code:
import random

names = []
for k in range(32):
newname = raw_input("Enter contestant's name: ")
names.append(newname)

slots = {}
for i in range(32):
slots[i] = random.choice(names)
names.remove(slots[i])

for count in range(32):
nessdash = 60 - len(slots[count])
print slots[count] + "-" * nessdash
if count and (count+1) % 4 == 0:
print "\n"

raw_input("Press any key to exit")

Regards, mawe

• lw22 agrees : mawe is good stuff

4. There needs to be way to correct mistakes in the input loop. For instance, if you accidentally just press enter, you have to retype the whole dang list! Most folks would consider that very user-unfriendly!

You could give the entry a number (might as well use k) and when you make a booboo then enter that number and get another shot at the correct name entry.
Last edited by Dietrich; September 13th, 2005 at 03:20 PM.
5. No Profile Picture
sfb
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
Nov 2003
Posts
624
Rep Power
38
a) You're typing all the names in anyway, why not put them in a text file? That way, if you want to run it again - there they are, no more typing.

b) Counter variables are not very pythonic - there's usually a way to do the same thing that makes the code shorter and easier to read as well. - e.g.

Code:
k = 1
names = [""]
while k < 33:
newname = raw_input("Enter contestant's name: ")
names.append(newname)
k += 1
could become:

Code:
names = []
while len(names) < 33:
newname = raw_input("Enter contestant's name: ")
names.append(newname)

Or possibly name them something more helpful such as "loop_counter" and "namelist_index", etc.

c) Reading into the dictionary. First - it's unnecessary because the random module can sort a list into random order for you with random.sort(<list>). Still, b) applies here if you want to do it this way:

Code:
slots = {}
i = 1
j = 32
while i < 33:
selection = random.randrange(0,j,1)
slots[i] = names[selection]
del names[selection]
i += 1
j -= 1
Could be

Code:
slots = {}
while names:
selection = random.randrange(0,j,1)
slots[i] = names[selection]
del names[selection]
There are three places where you count based on the list being 33 entries long, where code like the above will just work no matter what the list length is, and you only need to type '33' in at the start, to limit the length of the list. Changing the length then becomes a 1-place thing.

Though, using random.shuffle you could reduce that to:

Code:
random.shuffle(names)
slots = dict(enumerate(names))
But if you go to random.shuffle, there's not much point using a dictionary at all...

d) nessdash = 60 - len(slots[count])

Use the string justify methods (ljust() and rjust()) to pad them for you, e.g.

Code:
>>> "1".ljust(4, '*')
1***

>>> "111".ljust(4, '*')
111*
e) If you still needed to, use string multiplication here:

Code:
nessdash = 60 - len(slots[count])
while len(toprint) < 60:
toprint += "-"
Code:
>>> "x" * 4
xxxx
to change it to

Code:
nessdash = 60 - len(slots[count])
toprint += ("-" * nessdash)
Implicit loops are much faster than looping using "while" in Python.

f) Modulo!

Which gives me...

Python Code:

import random

names = [line.strip() for line in file('names.txt')]

random.shuffle(names)

for index, name in enumerate(names):
print name.ljust(60, '-')

if (index+1) % 4 == 0:
print
print

• Yegg` agrees : I just had to give you credit for all of that.
Last edited by sfb; September 13th, 2005 at 04:19 PM.
6. No Profile Picture
sfb
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
Nov 2003
Posts
624
Rep Power
38
Then, of course, there's the risk of taking it too far the other way:

Code:
import random

names = [name.strip().ljust(60, '-') for name in file('names.txt')]
random.shuffle(names)
names[::4] = ['\n\n'+name for name in names[::4]]
print '\n'.join(names)