OliverMilo OliverMilo - 1 year ago 65
Python Question

Can't get Python class code to work

I'm new to Python. Anyway, I was trying to make a 21 questions game, but my code won't work.

The error is:

Name 'plux' is not defined.

Here is the code:

from random import randint

class Game(object):
num = randint(1, 3)
def plux(x):
x += 1
return x
def minu(x):
x -= 1
def iff(i):
apple = 0
num = randint(1, 3)
if num == 1:
x = input('Can you eat it? ').lower()
if x == "yes" or "yeah":
print("test num 1")
apple = plux(apple)

elif num == 2:
print('test num 2')
elif num == 3:
print("test num 3")
a = Game()

Answer Source

When you call the function plux in the line apple = plux(apple), add a self. in front of the function call. In Python, self. is used as a reference to the object itself that is used within the class scope (any place inside of the Game class). In every function of a class, self has to be the first parameter (this is something I don't know why we must do, I'm sure there is a reason for it, but just follow this convention carefully).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download