I can’t understand why the ‘team’ term keeps coming up undefined?

Question:

While trying to complete an assignment, my result keeps coming up as partially correct in the ZyBooks system. I’ve tried everything I can possibly think of to solve the issue, and have no idea what else to try. Here are the instructions for the assignment:

class Team:
    def __init__(self):
        self.name = 'team'
        self.wins = 0
        self.losses = 0

    # TODO: Define get_win_percentage()
    def get_win_percentage(self):
        return team.wins / (team.wins + team.losses)
    # TODO: Define print_standing()
    def print_standing(self):
        print(f'Win percentage: {team.get_win_percentage():.2f}')
        if team.get_win_percentage() >= 0.5:
            print('Congratulations, Team', team.name,'has a winning average!')
        else:
            print('Team', team.name, 'has a losing average.')
  

if __name__ == "__main__":
    team = Team()
    user_name = input()
    user_wins = int(input())
    user_losses = int(input())
    
    team.name = user_name
    team.wins = user_wins
    team.losses = user_losses
    
    team.print_standing()

I’m passing all the auto-generated tests aside from the last three, and I can’t understand why? To Do’s have to be included as well.

Asked By: MORI

||

Answers:

As noted in comments, in the below you’ve used team rather than self.

   # TODO: Define get_win_percentage()
   def get_win_percentage(self):
       return team.wins / (team.wins + team.losses)
   # TODO: Define print_standing()
   def print_standing(self):
       print(f'Win percentage: {team.get_win_percentage():.2f}')
       if team.get_win_percentage() >= 0.5:
           print('Congratulations, Team', team.name,'has a winning average!')
       else:
           print('Team', team.name, 'has a losing average.')

Corrected:

    # TODO: Define get_win_percentage()
    def get_win_percentage(self):
        return self.wins / (self.wins + self.losses)
    # TODO: Define print_standing()
    def print_standing(self):
        print(f'Win percentage: {self.get_win_percentage():.2f}')
        if self.get_win_percentage() >= 0.5:
            print('Congratulations, Team', self.name, 'has a winning average!')
        else:
            print('Team', self.name, 'has a losing average.')
Answered By: Chris

Just to add to the answer of joshmeranda.
In the class, when you call the atribute of this object you use the "self" parameter. This parameter is used to reference the instance itself.
For example:
def print_wins(self): print(self.wins) to refence the instance

When you do:
team = Team()
you created a instance of team that is called "team". With this, you can print the numbers of win, for example: print(team.wins)
Or, call the function "print_wins":
team.print_wins()

Answered By: Samir
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.