Home > Software design >  Why my code won't run when I'm using def?
Why my code won't run when I'm using def?

Time:04-25

So, I started python 2 weeks ago. Something important bugs me, what I just don't understand.

When I have functions why my code stop running? Without defs it's working. In my example I can't call just kezdo(), because i get an empty console.

Process finished with exit code 0

Anyone can explain it with my example code?

def kerdes(self):
    self.open1 = self.open("kerdesek.txt")
    self.open2 = self.open1.read()
    print(self.open2)
    return kezdo(self)

def kezdo(self):
    print(self.open2)
    user = int(input("valasz egy kerdest: "))
    kerdesek = self.open2

    valaszok = ("egyvalasz", "ketvalasz")
    kv = {kerdesek[user]: valaszok[user]}

    print(kv)

if I add kezdo() then I get an error:

Error: kezdo() missing 1 required positional argument: 'self'

Thanks!

CodePudding user response:

you should call the functions. like this:

kerdes()
kezdo()

CodePudding user response:

I can only take a guess based on the limited information but it appears as if you are defining these methods (kezdo() and kerdes()) in some class.

In that case, you need to use self when calling these methods, or Python won't know what you mean.

It needs to be something along the lines of:

def kerdes(self):
    self.open1 = self.open("kerdesek.txt")
    self.open2 = self.open1.read()
    print(self.open2)
    return self.kezdo(self)
    #      ^^^^
    #      You need to use self here.
  • Related