0

I have problem with getting output from another function to use in a function. I don't know the syntax of function in python. How do i take a output of another function to use in a function when i define it. def hero_attribute(hero_selection()): #This syntax isn't accepted

#This program will calculate the damge of hero with stats
global hero_str_result
global hero_agi_result
global hero_int_result

def hero_selection():
    print """1. Life Stealer (strength hero)\n
    2. Phantom lancer (agility hero)\n
    3. Phantom Assassin (agility hero)\n
    4. Wrait King (strength hero) \n
    """

    print "Please enter hero selection: "
    hero_num = int(raw_input("> "))
    return hero_num

def hero_attribute(hero_selection()): #This syntax isn't accepted 
    if hero_num == 1: # Life stealer 
        hero_str = 25 
        hero_agi = 18 
        hero_int = 15 
        #Hero growth stats
        str_growth = 2.4 
        agi_growth = 1.9 
        int_growth = 1.75 

    elif hero_num == 2: # Phantom lancer 
        hero_str = 
        hero_agi = ?
        hero_int = ?
        #Hero growth stats
        str_growth = 2.4
        agi_growth = 1.9
        int_growth = 1.75

    elif hero_num == 3: # Phantom Assassin
        hero_str = ?
        hero_agi = ?
        hero_int = ?
        #Hero growth stats
    else: #Wraith King
        hero_str = ?
        hero_agi = ?
        hero_int = ?    
        #hero growth stats
        str_growth = ?
        agi_growth = ?
        int_growth = ?  
    return (hero_str,hero_agi,hero_int,str_growth,agi_growth,int_growth)

def hero_type(hero_num):
    if hero_num == 1:
        hero_type = "str"
    elif hero_num == 2
        hero_type = "agi"
    elif hero_num == 3
        hero_type = "agi"   
    else:
        hero_type = "str"


#the function will ask user what to do with the hero
def hero_build():
    print "What do you want to do with the hero?"
    print """1. Build hero with stat
        2. Build hero with item (not yet)
        3. Build hero with level
        """

    user_choice = int(raw_input("> "))
    if user_choice == 1:
        print "You want to build hero with stats!"
        print "Please enter number of stats that you want to add: "
        hero_stats = int(raw_input=("> "))

        hero_str, hero_agi, hero_int,str_growth,agi_growth,int_growth = hero_attribute() #This function will take the result of hero_str, hero_agi,hero_int
        hero_str_result = hero_str + str_growth * hero_stats
        hero_agi_result = hero_agi + agi_growth * hero_stats
        hero_int_result = hero_int + int_growth * hero_stats    


    return hero_str_result, hero_agi_result, hero_int_result


print "This is the result of your build: ", hero_build()

2 Answers 2

1

A function is a piece of code that receive arguments, and to those arguments you assign a name. For example:

def square(x):
    return x * x

this function computes the square of a number; this unknown number in the body of the function will be called x.

Once you have a function you can call it, passing the values you want as arguments... for example

print( square(12) )

will print 144 because it will call the function square passing x=12 and 12*12 is 144.

You can of course pass a function the result of calling another function, e.g.

def three_times(x):
    return 3 * x

print( square( three_times(5) ) )

will display 225 because the function three_times will be passed 5 and it will return 3*5, the function square will be passed 15 and will return 15*15.

In the function definition (the def part) you will always just have names for the parameters. What you want to pass to the function will be written at the call site.

Sign up to request clarification or add additional context in comments.

1 Comment

This more a comment on the OP's question statement rather than on your answer: The question seemed to me about the function declaration & definition, while your answer is about function composition. Can one include a function call (as opposed to just a function) in the declaration of a function? Anyway, the OP accepted it, so I guess function composition was what (s)he was looking for. Some questions are really confusing.
0

What you want is to be able to pass a function as argument. This, however, is already incorporated into python from design: you simply pass it as you pass any other argument.

Example:

def apply(f,x):
   return f(x)
def sq(x):
   return x*x
def cb(x):
   return x*x*x
apply(sq,2)
4
apply(cb,2)
8

Apply is defined with its first argument being a function. You know that only when you actually read the definition of apply. There you see that f is treated as a function, as opposed to x which is treated "as a number".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.