Reputation: 27
I'm using all programs that come stock with the GNU based OS (Precise Puppy 5.4)
if-elif-else
statement that works with the argument valueif
statement determines which string to returnOutside of the class I prompt user input that is passed to variable userinput
.
I then call the method with userinput
as an argument and assign the return to
a variable name variable
.
Then I print variable
.
First, I understand that there are other ways to achieve the same effect. The reason I don't use one of them is because I'm making a fairly large text adventure and will need to make a very large amount of decisions and assign a very large number of variables.
It would be much easier to work with the code if I were to classify the different sections
of the game (i.e.: a class named Player
and methods named stats
, inventory
, gold
, etc.) using
classes as categories and methods as specific areas
I know that the error is related to the fact that when I call the class.function
, the class has no return value. But the method is not called inside the class so there's no way to return the value from the method inside of the class but outside of the method.
class classname () :
def methodname (value) :
if value == 1 :
return "Hello World"
else :
return "Goodbye World!"
userinput = raw_input("Enter the number [1]\n")
variable = classname.methodname (userinput)
print (variable)
the console output
Enter the number [1] (this is the prompt)
1 (this is the user input)
(now the error)
Traceback (most recent call last):
File "filename.py", line 8, in <module>
variable = (classname.methodname (userinput))
TypeError: unbound method methodname() must be called with
classname instance as first argument (got classobj instance
instead)
This problem has been resolved. the issue was a simple syntax issue. here is the fixed code Props to max for the solution and Lev Levitsky for formatting this post! ^.^
class classname () :
def methodname (self, value) :
if value == "1" :
return "Hello World"
else :
return "Goodbye World!"
userinput = raw_input("Enter the number [1]\n")
variable = classname().methodname (userinput)
print (variable)
Upvotes: 1
Views: 138
Reputation: 2153
You are almost there. Just add @staticmethod
before def methodname(value)
;)
or, if you didn't intend to use a static method, try changing the signature of methodname to accept one additional parameter self (def methodname (self, value) :
) and be sure to always call methodname
from instances: variable = (classname().methodname (userinput))
.
Upvotes: 3