-3

I am making a text based game for class and I want to learn more complicated things while making it.

#imports
import random
import time

#Game setup
class speak:
  def QD(text, pause):
    print('Quandale Dingle: ' + text)
    time.sleep(pause)

  def JQ(text, pause):
    print('Jontavious Quangle Dangle: ' + text)
    time.sleep(pause)

  def TQ(text, pause):
    print('The Quantum Dangler: ' + text)
    time.sleep(pause)

#Game script
speak.QD("Greetings", 1)
speak.JQ("Hello", 1)
speak.TQ("Salutations my good lads", 1)

How can I add __init__() to this and would it be practical for this case?

martineau
  • 112,593
  • 23
  • 157
  • 280
  • 1
    Make the class name Speak (uppercase). then you need to instantiate the class before calling its methods. do `speak = Speak()`. if you need to do something when instantiating the class add a `__init__` method that will be automatically called when instantiating. Also yout methods need the decorator `@staticmethod` otherwise the first parameter must be `self`. – Sembei Norimaki May 13 '22 at 14:49
  • 2
    You need to understand first what the `__init__` method does and what it's for. You also need to re-check your tutorials on classes, because that code right now does not make much sense to be a class. – Gino Mempin May 13 '22 at 14:50
  • 1
    the way you are using your class at the moment, the methods should be decorated with `@staticmethod` https://docs.python.org/3/library/functions.html#staticmethod when you do this the class is just a "namespace" (i.e. a way of grouping things together) and has no particular utility itself – Anentropic May 13 '22 at 14:52
  • @GinoMempin I totally agree, classes which only have staticmethods should usually just be a module – Anentropic May 13 '22 at 14:55

0 Answers0