class tennis:
def __init__ (self, name, rank, tournamentsPlayed, country, racquetBrand):
self.name = name
self.rank = rank
self.tournamentsPlayed = tournamentsPlayed
self.country = country
self.racquetBrand = racquetBrand
#defines each thing^
def __str__(self):#creates a string
return self.name + ":" + self.rank + "\n" + self.tournamentsPlayed + "\n" + self.country + "\n" + self.racquetBrand
#prints information from above things
class professionals:
def __init__(self):
self.tennisPro = []
self.tennisPro.append(tennisPro("Roger Federer", "1", "18", "SUI", "Wilson"))
self.tennisPro.append(tennisPro("Novak Djokovic", "2", "21", "SRB", "Head"))
self.tennisPro.append(tennisPro("Rafael Nadal", "3", "17", "ESP", "Babolat"))
self.tennisPro.append(tennisPro("Juan Martin Del Potro", "4", "18", "ARG", "Wilson"))
self.tennisPro.append(tennisPro("Andy Murray", "5", "17", "GRB", "Head"))
self.tennisPro.append(tennisPro("Nikolay Davydenko", "6", "25", "RUS", "Prince"))
self.tennisPro.append(tennisPro("Robin Soderling", "7", "25", "SWE", "Head"))
self.tennisPro.append(tennisPro("Andy Roddick", "8", "20", "USA", "Babolat"))
self.tennisPro.append(tennisPro("Fernando Verdasco", "9", "26", "ESP", "Tecnifibre"))
self.tennisPro.append(tennisPro("Jo-Wilfried Tsonga", "10", "25", "FRA", "Wilson"))
def sort(self, x):
n = len(self.tennisPro)
if n <= 1:
return self.tennisPro
else:
return tennisPro.mergeSort(self.tennisPro, x)
#finds the length of the length of self.tennisPro and returns it if its 1 or less, if not it runs the mergeSort method
@staticmethod #makes it a class method
def mergeSort(l, x):
## print("Running MergeSort on {0}".format(l))
n = len(l)
if n <= 1:
return l
else:
i = n//2
a = tennisPro.mergeSort(l[0:i], x)
b = tennisPro.mergeSort(l[i:], x)
return tennisPro.merge(a, b, x)
#finds length of self.tennisPro ands returns its if its 1 or less, if not it runs it through merge and returns it
@staticmethod #makes it a class method
def merge(a , b, x):
## print("Running Merge on {0} and {1}".format(a, b))
index1 = 0
index2 = 0
temp = []
if x == "name":
while (index1<len(a)) and (index2<len(b)):
if a[index1].name <= b[index2].name:
temp.append(a[index1])
index1+=1
else:
temp.append(b[index2])
index2+=1
if index1 < len(a):
for i in a[index1:]:
temp.append(i)
elif index2 < len(b):
for i in b[index2:]:
temp.append(i)
if x == "rank":
while (index1<len(a)) and (index2<len(b)):
if a[index1].rank <= b[index2].rank:
temp.append(a[index1])
index1+=1
else:
temp.append(b[index2])
index2+=1
if index1 < len(a):
for i in a[index1:]:
temp.append(i)
elif index2 < len(b):
for i in b[index2:]:
temp.append(i)
if x == "tournamentsPlayed":
while (index1<len(a)) and (index2<len(b)):
if a[index1].tournamentsPlayed <= b[index2].tournamentsPlayed:
temp.append(a[index1])
index1+=1
else:
temp.append(b[index2])
index2+=1
if index1 < len(a):
for i in a[index1:]:
temp.append(i)
elif index2 < len(b):
for i in b[index2:]:
temp.append(i)
if x == "country":
while (index1<len(a)) and (index2<len(b)):
if a[index1].country <= b[index2].country:
temp.append(a[index1])
index1+=1
else:
temp.append(b[index2])
index2+=1
if index1 < len(a):
for i in a[index1:]:
temp.append(i)
elif index2 < len(b):
for i in b[index2:]:
temp.append(i)
if x == "racquetBrand":
while (index1<len(a)) and (index2<len(b)):
if a[index1].racquetBrand <= b[index2].racquetBrand:
temp.append(a[index1])
index1+=1
else:
temp.append(b[index2])
index2+=1
if index1 < len(a):
for i in a[index1:]:
temp.append(i)
elif index2 < len(b):
for i in b[index2:]:
temp.append(i)
## each one takes each variable and seperates it into 2 indexes, puts the greater into temp list, and merges with other list till temp is ordered
## for i in temp:
## print(i.name)
return temp
#returns list temp(ordered)
t = tennisPro()
s = t.sort("name")#sorts self.players depending on varialbe put in quotes
for i in t:#for i in sorted list
print(i)#prints answer
this is my code for a project i'm working on. however, when i run it, it says that t = tennisPro() is not defined. is there any way for me to fix this?
thanks in advance.