0

recently getting in to python and im finding classes a bit challenging.

Program below im just trying to simulate a simple dice roll to practise classes. Specifically not sure why i'm getting the error here where i can't seem to reference self.sides

Thanks!

import random  
class Dice(object):  
    def __init__(self,sides):  
        self.sides = sides    
    def getSides(self):  
        return self.sides  
    def __str__(self):  
        return  
    def RollDice(self):  
        # create list with each potential number  
        outcomes=[]  
        temp = self.sides()      **<----------- 'int' object is not callable**  
        for i in range(0,temp+1):  
            outcomes.append(i)  
        # select random number  
            roll = random.choice(outcomes)  
            print(roll)  

normal=Dice(6)  
normal.RollDice()  
3
  • 3
    Remove the (). You should use either = self.sides or = getSides(). Commented Dec 13, 2018 at 17:16
  • thanks that worked. Can you please explain why that is the case? Commented Dec 13, 2018 at 17:21
  • 1
    Using () is only for functions. getSides() is a function, thus it has parenthesis to accommodate parameters. A variable does not use parenthesis, so python thought you were looking for a function! Commented Dec 13, 2018 at 17:28

2 Answers 2

1

This has already been addressed in the comments but since you are just getting into python, I thought a proper answer that actually explained why you are making this change would be appropriate.

sides isn't a function. It is a number returned by the function call getSides(). The purpose of parentheses in python is to designate arguments for function calls. If there is just a () after a function, with nothing inside it, that means there are no arguments for that function, but it's still a function. it does something. One way to avoid this mistake is just to be cognizant of what your functions are, but, as that can be a bit tricky at the very beginning, one trick to use until you get more comfortable with functions is to imagine sticking an argument in those parentheses and see if it makes sense. For example. We have the function getSides(). If you put something in like getSides(self), that makes sense, because each die has multiple sides you would want to get. This is a function that does something. Now, what about side(self)? Well the side variable doesn't actually do anything. If side==6, that isn't going to change self at all. It's just a number, which was returned by another function, so no parentheses.

Sign up to request clarification or add additional context in comments.

1 Comment

No problem! Good luck and happy coding!
0

Here:

temp = self.sides()

The () is trying to call an int object. Remove it, and the code runs perfectly fine:

temp = self.sides

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.