so i'm almost done with my project. however, i'm still getting some errors when i run. you can find my code below. if you see any problems and know how to fix them, please, please, please let me know!

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 "Name: " + self.name + " Rank: " + self.rank + " Tournaments: " + self.tournamentsPlayed + " Country: " + self.country + " Racquet: " + self.racquetBrand
#prints information from above things

class tennisProfessionals:

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"))
#defines each characters term thingys

def sort(self, x):
n = len(self.tennisPro)
if n <= 1:
return 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):
n = len(l)
if n <= 1:
return l
else:
i = n//2
a = self.tennisPro.mergeSort(l[0:i], x)
b = self.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):
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

return temp
## returns list temp(ordered)


t = tennisProfessionals()
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


when it runs, it returns
Traceback (most recent call last):
File "E:\tennisPlayers.py", line 132, in <module>
t = tennisProfessionals()
File "E:\tennisPlayers.py", line 19, in __init__
self.tennisPro.append(tennisPro("Roger Federer", "1", "18", "SUI", "Wilson"))
NameError: global name 'tennisPro' is not defined

please help me :(

Recommended Answers

All 5 Replies

You forgot the code tags (# button before pasting).

so i'm almost done with my project. however, i'm still getting some errors when i run. you can find my code below. if you see any problems and know how to fix them, please, please, please let me know!

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 "Name: " + self.name + " Rank: " + self.rank + " Tournaments: " + self.tournamentsPlayed + " Country: " + self.country + " Racquet: " + self.racquetBrand
    #prints information from above things

class tennisProfessionals:
    
    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"))
        #defines each characters term thingys

    def sort(self, x):
        n = len(self.tennisPro)
        if n <= 1:
            return 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):
        n = len(l)
        if n <= 1:
            return l
        else:
            i = n//2
            a = self.tennisPro.mergeSort(l[0:i], x)
            b = self.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):
        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

        return temp
## returns list temp(ordered)


t = tennisProfessionals()
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

when it runs, it returns
Traceback (most recent call last):
File "E:\tennisPlayers.py", line 132, in <module>
t = tennisProfessionals()
File "E:\tennisPlayers.py", line 19, in __init__
self.tennisPro.append(tennisPro("Roger Federer", "1", "18", "SUI", "Wilson"))
NameError: global name 'tennisPro' is not defined

please help me :(

thanks... do you know how i can fix it now though? i mean, i keep running it and changing minor things, and it keeps giving me the same error :( the error where it says NameError: global name 'tennisPro' is not defined. even though i thought i defined it when i said t = tennisPro() at the very bottom of my code.

It's because your class isn't "tennisPro", it's "tennis"

self.tennisPro.append(tennisPro("Roger Federer", "1", "18", "SUI", "Wilson"))
needs to be
self.tennisPro.append(tennis("Roger Federer", "1", "18", "SUI", "Wilson"))

It is customary to start class names with an uppercase letter, this way you may not have made this mistake!

def sort(self, x):

It is bad form to re-define reserved words like "sort" so use a more descriptive name (sort what?) and avoid the confusion. Also, you should include one function at a time and test it, then add another, etc. And why are all of the functions staticMethods? Perhaps I just don't understand the logic.

thanks... do you know how i can fix it now though?

"thanks... do you know how i can fix it now though?"
Re-post the code as a new reply with code tags around it.

Looks like there is a problem with the new page form, i.e ["QUOTE"] does not work.

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.