Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在另一个函数中使用函数的输出作为python编码的新输入 #对于这些新东西,非常感谢您的帮助 def总基数(int1、int2、int3、int4): “”“(int,int,int,int)->integer 返回所覆盖的碱基总数 >>>总基数(2,3,4,5) 40 """ 返回int1+2*int2+3*int3+4*int4 def段塞百分比(总碱,int5): “”“(total_base,int5)->float#因此,例如,我需要第一个函数的乘积作为第二个函数的乘积 返回给定所覆盖的碱基总数和bat(int5)时的总数的缓动百分比 >>>段塞流_百分比(20,9) 2.22 """ 返回总基数/int5 def在基础上的百分比(h、bb、hbp、ab、sf): “”(int,int,int,int,int)->float 返回给定命中率的垒上百分比,基于球,按球距命中,在击球和圣器命中 >>>基本百分比(1,2,3,4,5) 0.43 """ 返回(h+bb+hbp)/(ab+bb+hbp+sf) def在基础上加上缓动(在基础上百分比,缓动百分比): “”(float,float)->float# 返回给定基上百分比和段塞百分比的基上加段塞 >>>在基础上加上缓动(1.0,2.0) 3 """ 基本收益率+缓动收益率 def OPS_值(在_基础上加上_缓动): “”“(浮点)->字符串 返回给定on base plus SLUGING分数范围的on base plus SLUGING值 >>>操作单元值(0.8234) B """ 如果在基础上加上缓动>0.9000: 返回“A” elif在基础上加上缓动>0.7667: 返回“B” elif在基础上加上缓动>0.7000: 返回“C” 否则,在基础上加上缓动_Python_Function_Input_Output - Fatal编程技术网 float#因此,例如,我需要第一个函数的乘积作为第二个函数的乘积 返回给定所覆盖的碱基总数和bat(int5)时的总数的缓动百分比 >>>段塞流_百分比(20,9) 2.22 """ 返回总基数/int5 def在基础上的百分比(h、bb、hbp、ab、sf): “”(int,int,int,int,int)->float 返回给定命中率的垒上百分比,基于球,按球距命中,在击球和圣器命中 >>>基本百分比(1,2,3,4,5) 0.43 """ 返回(h+bb+hbp)/(ab+bb+hbp+sf) def在基础上加上缓动(在基础上百分比,缓动百分比): “”(float,float)->float# 返回给定基上百分比和段塞百分比的基上加段塞 >>>在基础上加上缓动(1.0,2.0) 3 """ 基本收益率+缓动收益率 def OPS_值(在_基础上加上_缓动): “”“(浮点)->字符串 返回给定on base plus SLUGING分数范围的on base plus SLUGING值 >>>操作单元值(0.8234) B """ 如果在基础上加上缓动>0.9000: 返回“A” elif在基础上加上缓动>0.7667: 返回“B” elif在基础上加上缓动>0.7000: 返回“C” 否则,在基础上加上缓动,python,function,input,output,Python,Function,Input,Output" /> float#因此,例如,我需要第一个函数的乘积作为第二个函数的乘积 返回给定所覆盖的碱基总数和bat(int5)时的总数的缓动百分比 >>>段塞流_百分比(20,9) 2.22 """ 返回总基数/int5 def在基础上的百分比(h、bb、hbp、ab、sf): “”(int,int,int,int,int)->float 返回给定命中率的垒上百分比,基于球,按球距命中,在击球和圣器命中 >>>基本百分比(1,2,3,4,5) 0.43 """ 返回(h+bb+hbp)/(ab+bb+hbp+sf) def在基础上加上缓动(在基础上百分比,缓动百分比): “”(float,float)->float# 返回给定基上百分比和段塞百分比的基上加段塞 >>>在基础上加上缓动(1.0,2.0) 3 """ 基本收益率+缓动收益率 def OPS_值(在_基础上加上_缓动): “”“(浮点)->字符串 返回给定on base plus SLUGING分数范围的on base plus SLUGING值 >>>操作单元值(0.8234) B """ 如果在基础上加上缓动>0.9000: 返回“A” elif在基础上加上缓动>0.7667: 返回“B” elif在基础上加上缓动>0.7000: 返回“C” 否则,在基础上加上缓动,python,function,input,output,Python,Function,Input,Output" />

在另一个函数中使用函数的输出作为python编码的新输入 #对于这些新东西,非常感谢您的帮助 def总基数(int1、int2、int3、int4): “”“(int,int,int,int)->integer 返回所覆盖的碱基总数 >>>总基数(2,3,4,5) 40 """ 返回int1+2*int2+3*int3+4*int4 def段塞百分比(总碱,int5): “”“(total_base,int5)->float#因此,例如,我需要第一个函数的乘积作为第二个函数的乘积 返回给定所覆盖的碱基总数和bat(int5)时的总数的缓动百分比 >>>段塞流_百分比(20,9) 2.22 """ 返回总基数/int5 def在基础上的百分比(h、bb、hbp、ab、sf): “”(int,int,int,int,int)->float 返回给定命中率的垒上百分比,基于球,按球距命中,在击球和圣器命中 >>>基本百分比(1,2,3,4,5) 0.43 """ 返回(h+bb+hbp)/(ab+bb+hbp+sf) def在基础上加上缓动(在基础上百分比,缓动百分比): “”(float,float)->float# 返回给定基上百分比和段塞百分比的基上加段塞 >>>在基础上加上缓动(1.0,2.0) 3 """ 基本收益率+缓动收益率 def OPS_值(在_基础上加上_缓动): “”“(浮点)->字符串 返回给定on base plus SLUGING分数范围的on base plus SLUGING值 >>>操作单元值(0.8234) B """ 如果在基础上加上缓动>0.9000: 返回“A” elif在基础上加上缓动>0.7667: 返回“B” elif在基础上加上缓动>0.7000: 返回“C” 否则,在基础上加上缓动

在另一个函数中使用函数的输出作为python编码的新输入 #对于这些新东西,非常感谢您的帮助 def总基数(int1、int2、int3、int4): “”“(int,int,int,int)->integer 返回所覆盖的碱基总数 >>>总基数(2,3,4,5) 40 """ 返回int1+2*int2+3*int3+4*int4 def段塞百分比(总碱,int5): “”“(total_base,int5)->float#因此,例如,我需要第一个函数的乘积作为第二个函数的乘积 返回给定所覆盖的碱基总数和bat(int5)时的总数的缓动百分比 >>>段塞流_百分比(20,9) 2.22 """ 返回总基数/int5 def在基础上的百分比(h、bb、hbp、ab、sf): “”(int,int,int,int,int)->float 返回给定命中率的垒上百分比,基于球,按球距命中,在击球和圣器命中 >>>基本百分比(1,2,3,4,5) 0.43 """ 返回(h+bb+hbp)/(ab+bb+hbp+sf) def在基础上加上缓动(在基础上百分比,缓动百分比): “”(float,float)->float# 返回给定基上百分比和段塞百分比的基上加段塞 >>>在基础上加上缓动(1.0,2.0) 3 """ 基本收益率+缓动收益率 def OPS_值(在_基础上加上_缓动): “”“(浮点)->字符串 返回给定on base plus SLUGING分数范围的on base plus SLUGING值 >>>操作单元值(0.8234) B """ 如果在基础上加上缓动>0.9000: 返回“A” elif在基础上加上缓动>0.7667: 返回“B” elif在基础上加上缓动>0.7000: 返回“C” 否则,在基础上加上缓动,python,function,input,output,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 """

将变量另存为全局变量是一种方法

# 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"
另一种方法,我相信您正在寻找的是在函数中调用函数。这看起来像这样:

 def some_function():
      global var     #This is a global variable
      #Do things to variable
这将使变量
num
=5*2


我希望这有帮助。

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

我想您正试图使用
on\u base
slaging
百分比来计算
OPS\u值

因此,您可以计算
基数
总基数
,以及
缓动
,并将返回的值存储在变量中

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

参见下面的示例:

 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
我们试图做的是将与计算每件事相关的数学分开


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

请格式化此代码。那么问题是什么?@sheeptest,我认为最好省略此代码,因为OP询问的不是代码。他在问关于在函数中调用函数的问题,对吗?@T.Woody是的。你说得对。我只是不知道当它未格式化时,里面是否有一个文本问题。sheeptest提出的解决方案比在另一个函数中使用一个函数要干净得多。定义清晰输入和输出的函数是更大更复杂代码的良好构建块。它们可以在不同的环境中重复使用,并且更易于测试。当然,在另一个函数中调用函数是一种非常有效的方法,并且仍然有效。只是风格问题。
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