WonkasWilly
WonkasWilly

Reputation: 47

Why isn't this function working?

Just trying to play around with some LPTHW functions. I made this up here:

def character_class(intel, str, agil):
    print "A barbarian's main attribute starts at: %d." % str
    print "A wizard's main attribute is intel, and it starts at: %d." % intel
    print "An archer's main attribute is agility, and has the default agility speed of: %d.\n" % agil


character_class(20, 40, 60)
print character_class

character_class(20 + 40, 40 + 50, 100 + 100)
print character_class

input1 = raw_input("Barbarian str:")
input2 = raw_input("Wizard intel:")
input3 = raw_input("Archer agil:")
character_class % (input1, input2, input3)
print character_class

And these are the results I'm getting in Powershell:

A barbarian's main attribute starts at: 40.
A wizard's main attribute is intel, and it starts at: 20.
An archer's main attribute is agility, and has the default agility speed of: 60.

<function character_class at 0x025078B0>
A barbarian's main attribute starts at: 90.
A wizard's main attribute is intel, and it starts at: 60.
An archer's main attribute is agility, and has the default agility speed of: 200.

<function character_class at 0x025078B0>
Barbarian str:200
Wizard intel:300
Archer agil:400
Traceback (most recent call last):
  File "test19.py", line 16, in <module>
    character_class % (input1, input2, input3)
TypeError: unsupported operand type(s) for %: 'function' and 'tuple'

First of all, what is <function character_class at 0x025078B0> that is appearing after every call to the function character_class? This didn't appear during LPTHW exercise 19.

Also, I'm trying to get raw_input from the user, to insert into the function. Is this not possible, or am I doing it wrong?

Revision: Changed the last line of code to: character_class(input1, input2, input3)

And this is the error I get now:

A barbarian's main attribute starts at: 40.
    A wizard's main attribute is intel, and it starts at: 20.
    An archer's main attribute is agility, and has the default agility speed of: 60.

    A barbarian's main attribute starts at: 90.
    A wizard's main attribute is intel, and it starts at: 60.
    An archer's main attribute is agility, and has the default agility speed of: 200.

    Barbarian str:1000
    Wizard intel:2000
    Archer agil:3000
    Traceback (most recent call last):
      File "test19.py", line 16, in <module>
        character_class(input1, input2, input3)
      File "test19.py", line 2, in character_class
        print "A barbarian's main attribute starts at: %d." % str
    TypeError: %d format: a number is required, not str

Final revision:

def character_class(intel, str, agil):
    print "A barbarian's main attribute starts at: %d." % str
    print "A wizard's main attribute is intel, and it starts at: %d." % intel
    print "An archer's main attribute is agility, and has the default agility speed of: %d.\n" % agil


character_class(20, 40, 60)


character_class(20 + 40, 40 + 50, 100 + 100)


input1 = raw_input("Barbarian str:")
input2 = raw_input("Wizard intel:")
input3 = raw_input("Archer agil:")
inputa = int(input1)
inputb = int(input2)
inputc = int(input3)

character_class(inputa, inputb, inputc)

Upvotes: 0

Views: 89

Answers (3)

daniel
daniel

Reputation: 85

Firstly, lets look at your output:

A barbarian's main attribute starts at: 40.
A wizard's main attribute is intel, and it starts at: 20.
An archer's main attribute is agility, and has the default agility speed of: 60.

<function character_class at 0x025078B0>.

And then the code that generates this output:

character_class(20, 40, 60)
print character_class

You have defined the character_class function above to take three parameters and call the print function three separate times. When you call character_class(20, 40, 60), you are passing in 20, 40 and 60 as your parameters. Your function will then call print three times, using a newline feed in the last call, resulting in the first four lines of your output shown.

When you call print character_class, you are passing in a function definition as a parameter to the print function. The output, <function character_class at 0x025078B0>, is the reference location of the defined function. The point being, you do not need to call print before your character_class call, as you have defined your function to call print three times. When python is executing character_class(20,40,60), it will step into the function and execute each line of code you defined and insert the parameters you passed using string substitution. The additional print character_class is not necessary, as your function code does the print-ing for you.

Lastly, you are obtaining your raw input correctly. The raw_input function will take your input from the console and return the result, which you have assigned into a variable. Your call (character_class % (input1, input2, input3)) is almost correct, however I am thinking you are mixing up calling a function with string variable substitution.

You only need to call your function by passing the three parameters you obtained from input: character_class(input1, input2, input3) and your function will do the printing for you, as you have defined it to do above!

Upvotes: 0

Mochamethod
Mochamethod

Reputation: 276

I am not too knowledgeable in regards to unicode. I believe it to be reference code for specific characters on a keyboard and what not. Also, your problem was not too clear. However, I wrote this, hopefully it somewhat reformats and answers a couple questions you had:

class character_class(object):
def __init__(self, intel, str, agil):
    self.intel = intel
    self.str = str
    self.agil = agil

def input(self):
    str = input1
    intel = input2
    agil = input3
    print "A barbarian's main attribute starts at: %r." % str
    print "A wizard's main attribute is intel, and it starts at: %r." % intel
    print "An archer's main attribute is agility, and has the default agility speed of: %r.\n" % agil
    cont = raw_input("")

input1 = raw_input("Barbarian str:")
input2 = raw_input("Wizard intel:")
input3 = raw_input("Archer agil:")
character_class = character_class(input1, input2, input3)
character_class.input()

I'm still rather new to programming, so my code might be a bit 'off' or sloppy or whatever. Anyway, I assigned intel, str, and agil to a class property, and then defined those through raw input.

When you run this script, it will prompt you for the attributes under input 1-3, and then display them through character_class.input(). Those input values are assigned to str, intel, and agil. Did this answer your question in any way? If not let me know.

Upvotes: -1

Daniel Roseman
Daniel Roseman

Reputation: 600041

You're successfully calling the function, which does the printing, and then each time you also tell Python to print the function itself, when you do print character_class. Just don't do that.

Also, I don't know why you've used % in the last one, where you're passing in the data from raw_input. Again, don't do that:

character_class(input1, input2, input3)

Upvotes: 2

Related Questions