Hi there :> I'm learing python object programming. I've made simple code:
import random
drawn = []
class Lottery():
def __init__(self, letter, drawn):
self.letters = ['a', 'b', 'c', 'd', 'f', 'g', 'h']
self.letter = letter
self.drawn = drawn
def drawnumbers():
number = random.randint(0, 10)
return number
def drawletters(self):
letter = random.choice(self.letters)
return letter()
for i in range(0,9):
drawn.append(drawnumbers())
for i in range(0,3):
drawn.append(drawletters())
print(drawn)
This is the error:
Traceback (most recent call last):
File "C:/Users/pawee/PycharmProjects/pythonProject1/lottery.py", line 3, in <module>
class Lottery():
File "C:/Users/pawee/PycharmProjects/pythonProject1/lottery.py", line 19, in Lottery
drawn.append(drawletters())
TypeError: drawletters() missing 1 required positional argument: 'self'
What i did wrong?
CodePudding user response:
So when you're defining methods that are a part of a class, they all have 'self' as the first parameter.
You also don't include parenthesis when you define the class unless it's inheriting from another class
However, there are a couple more errors.
I think it's probably a formatting error, but the last couple of lines need to be unindented so they're outside of the class.
If they're supposed to be a part of the class, then you're gonna want to put them in their own methods (also including the self parameter).
In order to make the calls to a lottery class work without an instance, they have to be static as well.
So getting it to run would involve making your code look like this...
import random
drawn=[]
class Lottery: #no parenthesis
def __init__(self, letter, drawn):
self.letters = ['a', 'b', 'c', 'd', 'f', 'g', 'h']
self.letter = letter
self.drawn = drawn
@staticmethod
def drawnumbers(self): #include self
number = random.randint(0, 10)
return number
@staticmethod
def drawletters(self): #include self
letter = random.choice(self.letters)
return letter #no parenthesis
#unindent so we aren't in the class anymore
for i in range(0,9):
drawn.append(Lottery.drawnumbers())
for i in range(0,3):
drawn.append(Lottery.drawletters())
print(drawn)
#at this point, drawn is not associated with the lottery class
# to associate it, you need to make a variable out of the class
lotteryObj = Lottery("A", drawn) #it takes 2 arguments, letter and drawn
print(lotteryObj.drawn) #=> [...]
print(lotteryObj.letter) #=> "A"
However, this isn't really how classes should work.
If you want to make a lottery class, it would be a good idea to have that class hold the data it uses, and have methods that effect that data.
This is where default parameters come in handy. They let you define parameters for your class that don't need to be given.
This way, you can have a list of some drawn lottery numbers, give it to your lottery class, and then have it add to the list with more numbers/letters drawn.
import random
drawn=[]
class Lottery: #no parenthesis
def __init__(self, drawn=[]): #default for drawn, letter not needed
self.letters = ['a', 'b', 'c', 'd', 'f', 'g', 'h']
self.drawn = drawn #if you don't give the class a list, it makes an empty list
def drawnumbers(self): #include self
number = random.randint(0, 10)
#instead of returning, change the list this class has using self
self.drawn.append(number)
def drawletters(self): #include self
letter = random.choice(self.letters)
self.drawn.append(letter)
#you can add a print method or get methods for printing or getting the drawn list
#unindent so we aren't in the class anymore
lotto1 = Lottery()
lotto2 = Lottery([1]) #give it a list with something in it
for i in range(0,9):
lotto1.drawnumbers()
lotto2.drawnumbers()
for i in range(0,3):
lotto1.drawletters()
lotto2.drawletters()
print(lotto1.drawn)
print(lotto2.drawn)