-1

Usually, one is able to run a method without providing an argument for the parameter "self", however, I'm unable to do that.

class Dice:
    def roll(self):
        x = (range(1, 7))
        y = (range(1, 7))
        from random import choice
        x1 = choice(x)
        y2 = choice(y)
        output = (x1, y2)
        return output


dice = Dice
dice.roll() # here it shows that parameter self is required

The Error shown is: dice.roll() # here it shows that parameter self is ^^^^^^^^^^^ TypeError: Dice.roll() missing 1 required positional argument: 'self'

tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • 4
    `dice = Dice()` – Unmitigated Mar 26 '23 at 23:06
  • 1
    As mentioned, you assigned the class to `dice`, but really you needed to instantiate a new instance of the class. – tdelaney Mar 26 '23 at 23:06
  • @Muhammad Tayyab Ullah I think you have only missed the couple of parenthesis `()` but an other way to call the method `roll()` is by `Dice.roll(Dice)`. If you want check [this post](https://stackoverflow.com/questions/75823739/python-class-method-invocation-missing-1-required-positional-argument/75832174#75832174) or [this other](https://stackoverflow.com/questions/75833297/access-the-attributes-of-a-python-class-by-reference). – frankfalse Mar 27 '23 at 09:48

1 Answers1

1

You need to initialize the class first.

dice = Dice()
dice.roll()

would work.

Or you could add the staticmethod decorator to your function

class Dice:
    @staticmethod
    def roll():
        x = (range(1, 7))
        y = (range(1, 7))
        from random import choice
        x1 = choice(x)
        y2 = choice(y)
        output = (x1, y2)
        return output
TimbowSix
  • 342
  • 1
  • 7