-1

With this code I want to generate a tuple of random numbers. I know that there are easy ways to do it without using classes but I want the code to have class too.

import random

class Dice:
    def roll(self):
        generate = random.randint(1, 6)
        generate2 = random.randint(1, 6)
        return generate, generate2

dice = Dice
print(dice.roll())

It generates this error:

print(dice.roll()) 
TypeError: roll() missing 1 required positional argument: 'self'

and when I change my code like this : print(dice.roll(self)) it creates another error that the self name is not defined.

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
  • Try looking at this answer for static class methods. https://stackoverflow.com/questions/735975/static-methods-in-python The issue is that the roll() method expects self, using a static method removes this expectation. – RetardedHorse Feb 23 '21 at 07:01

1 Answers1

5
import random

class Dice:
    def roll(self):
        generate = random.randint(1, 6)
        generate2 = random.randint(1, 6)
        return generate, generate2

dice = Dice() <----you omitted this
print(dice.roll())
Ade_1
  • 1,480
  • 1
  • 6
  • 17