'Why my code won't run when I'm using def?
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!
Solution 1:[1]
you should call the functions. like this:
kerdes()
kezdo()
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Berkehan Yava? |
| Solution 2 | Paul P |
