Hello!

Ive been working on a little python program on a newbie course in python and ive come this far that ive created a program that reads from a file and counts the lines of the file (each line a players name with some stats). Then it asks for how many competitions you want and the program loops as many times as games asked.. anyhow the program uses a formula to count and give each player points each game depending on what numbers they get randomly given..

Ive come so far that ive been able to do all this but not how to sort out the winners from a list and count how many times they have actually won. Then i have the part where iam supposed to look for if any other player has the same amount of points in a serie and they will have to "reshoot" and see who the winner really is. Anyhow here is the code.

Please be gentle iam a newbie on python :D

import random

class Game(object):

    def __init__(self):
        self.players()
    
    def players(self):

        participants = 0
        the_file = open("players.txt", "r")
        for line in the_file:
            participants += 1
        the_file.close()

        the_file = open("players.txt", "r")
        wholefile = the_file.read()
        wholefile = wholefile.replace("/", "\n")
        the_file.close()
        print "P l a y e r s  &  S t a t i s t i c s\n"
        print participants, "players.\n"
        print wholefile

        games = int(raw_input("How many games do you want?: "))

        self.play(participants, games)

    def shooting(self, shots):
        
        for skott in range(10):
                skott = random.randrange(1, 100)

                if 0 < skott < 90:

                    p = (110 - skott)/20

                    shots.append(p)
                    
                elif skott > 90:
                    p = 0 
                    shots.append(p)

        del shots[8:]
        return shots
        
    def play(self, participants, games):

        winner = []
        
        partic = participants

        print "\n"
        
        while games > 0:

            playerdict = {}
            the_file = open("players.txt", "r")
            games -= 1

            participants = partic
            
            while participants > 0:

                participants -= 1
                
                shots = []
                
                self.shooting(shots)

                points = sum(shots)
                
                readfile = the_file.readline()
        
                stringfile = ' '.join(readfile)
                
                slashposition = stringfile.find('/')
                
                print "Player: ", stringfile[:slashposition], " shoots.""\n"
                
                for a in shots:
                    print a,
                    
                print "\n\nResult: ", points,

                print "\n"
                
                playerdict[stringfile[:slashposition]] = points
            

            sortplayer = playerdict.items()

            sortplayer.sort(lambda x,y: cmp(x[1],y[1]), reverse = True)
            
            print "\n S t a t i s t i c s "
            
            for name, point in playerdict.items():
                print "\n", '%-15s Score: %2d' % (name, point)

            print "\n", sortplayer
            
            #stringfile = ' '.join(sortplayer)
            
            #playername = stringfile.find(",")
            
            print "\nWinner is: ", sortplayer[0]

            winner.append(sortplayer[0])
            
            print "\n", winner

            if games > 0:
                raw_input("Press enter for the next competition: ")

            
        the_file.close()
        
def main():

    print "Welcome to the shootinggame!\n"
    
    Game()

main()

I'm sorry ... I'm really unclear about the rules of this game.

I notice a couple of things that you can streamline.

* The lines

del shots[8:]
return shots

can be reduced to

return shots[8:]

because you don't want to actually change the shots array within a function (rule of thumb: don't change mutable objects within a function unless you have a darn good reason ... and then be prepared for bugs.)

* The little dance where you assign "partic = participants" and then later "participants = partic" is unnecessary ... just use participants directly and omit the partic variable.

I hope that helps a bit. If I can understand the rules of the game better, I will understand your main question better.

Jeff

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.