Hi!
I've made a small program for an assignment and it works as it should. There's just one problem; the code contains two global variables. I won't pass the course unless I get rid of them and it's been bugging me for weeks.
The program:
##This program simulates a series of shooting competitions with participants found in a text file.
#modules
import random, time, sys
# classes
class Participant(object):
"""person with statistics"""
# constructor
def __init__(self, name, sigma, competitions, wins, wa_results):
"""Create participant with attributes"""
self.name = name
self.sigma = sigma
self.competitions = competitions
self.wins = wins
self.wa_results = wa_results
# methods
def __str__(self):
"""String representation of the class."""
return self.name + "/" + self.sigma + "/" + self.competitions + "/" + self.wins + "/" + self.wa_results
# functions
def read_file(database):
"""takes the information from the textfile and returns a list"""
file1=[]
for line in database.readlines():
stats = line.split('/')
file1.append(stats)
return file1
def make_object(file1):
"""makes an instance for each contestant and runs the shooting function"""
for h in file1:
try:
temp = Participant(h[0],h[1],h[2],h[3],h[4])
shooting(h[0], h[1])
except ValueError and IndexError:
print "Wrong format."
time.sleep(1)
sys.exit()
def season(file1):
"""creates one season, uses the shooting method within read_file function"""
while True:
try:
comps_amount = int(raw_input("How many competitions in a season?"))
if comps_amount > 0 and comps_amount < 101:
break
else:
print "Must be at least 1 competition and at most 100."
except ValueError:
print "Try again with an integer this time..."
winnerlist=[]
print "======================================================="
for comp in range(1, comps_amount+1):
print "Competition nr:", comp, "\n"
global old_leader
old_leader=0
global tiedshot
make_object(file1)
print "======================================================="
if (comp+1)%3==True and comp!=1:
time.sleep(4)
winnerlist.append(leader)
print "Statistics:"
for word in set(winnerlist):
"""counts how many times a name appears in winnerlist"""
wins=winnerlist.count(word)
print word, "won", wins,
if wins==1:
print "competition."
else:
print "competitions."
def shooting(name, sigma):
"""creates one series of shots and results"""
print name,
shotlist=[]
for x in range(10):
try:
p = (110 - abs(int(random.normalvariate(0, int(sigma)))))
except ValueError:
print "Wrong format."
time.sleep(1)
sys.exit()
if p < 20:
p=0
elif p == 110:
p=100
shot = p/20
shotlist.append(shot)
print shot,
shotlist.sort()
del shotlist[0:2]
shotsum = sum(shotlist)
print "results =", shotsum
global old_leader
if shotsum > old_leader:
global leader
leader = name
old_leader = shotsum
def main():
"""main function"""
while True:
while True:
print """
1. Simulate a season of shooting competitions.
2. Exit programme.
"""
try:
choice = int(raw_input("Enter your choice:"))
except ValueError:
choice=None
if choice==1:
break
elif choice==2:
print "Good bye."
time.sleep(1)
sys.exit()
else:
print "Choose 1 or 2..."
try:
database = open("participants.txt", 'r')
file1= read_file(database)
database.close()
season(file1)
except IOError:
print "Can't find participants.txt, exiting."
time.sleep(1)
sys.exit()
# start program
main()
The txt-file that comes with, called participants.txt:
Bob/25/0/0/0
Bill/23/0/0/0
Billy Bob/32/0/0/0
Dick Cheney/34/0/0/0
Lee Harvey/28/0/0/0
Any help with how to replace the two global variables leader and old_leader would be much appreciated.
/Hydobat