0

How can I fix this problem in order to print playerValue as an integer value and as output? Here is the code that I've written:

class FootballPlayer:
    def __init__(self,na,ag,sh,pa,ta,sa):
        self.name = na
        self.age = ag
        self.shooting = sh
        self.passing = pa
        self.tackling = ta
        self.saving = sa
    def defenderScore(self):
        return int(0.80 * self.tackling + 0.15 * self.passing + 0.05 * self.shooting)
    def midfielderScore(self):
        return int(0.50 * self.passing + 0.25 * self.shooting + 0.25 * self.tackling)
    def forwardScore(self):
        return int(0.70 * self.shooting + 0.25 * self.passing + 0.05 * self.tackling)
    def goalieScore(self):
        return int(0.90 * self.saving + 0.10 * self.passing)
    def playerValue(self):
        return int(15000 * self.defenderScore ** 2 + 25000 * self.midfielderScore ** 2 
               + 20000 * self.forwardScore ** 2 + 5000 * self.goalieScore ** 2 
               - 30000 * (self.age - 26) ** 2)
    def __str__(self):
        return ("(" + str(self.name) + "," + str(self.age) + "," + str(self.playerValue) + ")")

ronaldo = FootballPlayer("Ronaldo", 36, 85, 95, 35, 5)
print(ronaldo)

Expected Output:

(Ronaldo,36,322445000)

The output I got:

(Ronaldo,36,<bound method FootballPlayer.playerValue of <__main__.FootballPlayer object at 0x7f0f4b901990>>)
Nisa Aslan
  • 37
  • 4
  • 2
    `playerValue` is a method; you need to call it. You might want to make it a property, though. – chepner May 24 '21 at 13:54

1 Answers1

3

When you want the value of a method, you need to call the method. playerValue() instead of playerValue. This is different from attributes, which do not need "do" anything, they should hold a value. But methods need to do some operations and then return the value. As you found out, the same issue persists in the rest of your code. Whenever you want to use the return value from a method, you need to call the method by using method() rather than method.

This has important implications in terms of usage and performance, especially for larger code bases. For a user, you probably want to make some values available in the same manner, e.g. as properties. To avoid having to recalculate a property every time, you can make used of cached properties. Those will be calculated only once and then cached, making your code much faster depending on the use case.

Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239