使用函数的输出作为另一个函数python的新输入编码

时间:2014-09-25 23:42:15

标签: python function input output

# any help is greatly appreciated new to this stuff 

def total_bases(int1,int2,int3,int4):
    """(int, int, int, int) -> integer
    Return the total number of bases covered 
    >>>total_bases(2,3,4,5)
    40
    """

    return int1+2*int2+3*int3+4*int4

def slugging_percentage(total_bases,int5):
    """ (total_bases, int5) -> float # so for instance i need the product of the first function for the second function 
    Return the slugging percentage given the total number of bases covered and the total number at bat(int5)
    >>>slugging_percentage(20,9)
    2.22
    """

    return total_bases/int5

def on_base_percentage(h,bb,hbp,ab,sf):
    """(int,int,int,int,int) -> float
    Return the on-base percentage given the hits, base on balls, hit by pitch, at bats and sacrfice hits
    >>>on_base_percentage(1,2,3,4,5)
    0.43
    """

    return (h+bb+hbp)/(ab+bb+hbp+sf)

def on_base_plus_slugging(on_base_percentage,slugging_percentage):
    """(float,float) -> float # as well as for this 
    Return the on-base plus slugging given the on-base percentage and the slugging percentage 
    >>>on_base_plus_slugging(1.0,2.0)
    3.0
    """

    return on_base_percentage+slugging_percentage

def OPS_value(on_base_plus_slugging):
    """(float) -> string
    Return the on-base plus slugging value given the on-base plus slugging score range 
    >>>OPS_value(0.8234)
    B
    """
if on_base_plus_slugging > 0.9000:
    return "A"
elif on_base_plus_slugging > 0.7667:
    return "B"
elif on_base_plus_slugging > 0.7000:
    return "C"
else on_base_plus_slugging < 0.7000:
    return "F"
elif on_base_plus_slugging == 0.7000:
    return "F"

2 个答案:

答案 0 :(得分:1)

函数可以返回值。您可以将这些值存储为变量。然后,您可以将这些值用作其他函数的输入。

我想您正在尝试使用OPS_valueon_base百分比来计算slugging

因此,您需要计算on_basetotal_basesslugging,并将返回的值存储在变量中。

然后将这些变量作为输入传递给OPS_value函数,该函数返回最终的计算值。

请参阅以下示例:

def OPS_value(percent):
    """(float) -> string
    Return the on-base plus slugging value given the on-base plus slugging score range 
    >>>OPS_value(0.8234)
    B
    """

    if percent > 0.9000:
        return "A"
    elif percent > 0.7667:
        return "B"
    elif percent > 0.7000:
        return "C"
    else:
        return "F"

total_bases = total_bases(2, 3, 4, 5) # Get the return value for total_bases

slugging = slugging_percentage(total_bases, 9) # Get the return value for slugging_percent
on_base = on_base_percentage(1, 2, 3, 4, 5) 
print OPS_value(on_base + slugging) # using on_base + slugging as input

我们要做的是保持与计算每个事物total_basesslugging等相关的数学。

原始代码的另一个主要变化是您不需要只添加两个值的函数。 您可以而且应该在一行中执行此操作。

答案 1 :(得分:-1)

将变量保存为全局是一种方式。

 def some_function():
      global var     #This is a global variable
      #Do things to variable

另一种方式,我认为你正在寻找的是调用函数内部的函数。这看起来像这样:

 def function_1(#some variable):
      #Stuff could be up here
      num = function2(5)
      #Stuff could be down here

 def function_2(a_number):
      a_number = a_number*2
      return a_number

这将使变量num = 5 * 2。

我希望这会有所帮助。