使用函数的输出作为另一个函数python中的输入来编码 [英] using the output of a function as the input in another function python new to coding

查看:201
本文介绍了使用函数的输出作为另一个函数python中的输入来编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #任何帮助都非常感谢新的东西

def total_bases(int1,int2,int3,int4):
( int,int,int,int) - >整数
返回覆盖的总数
>>> total_bases(2,3,4,5)
40
$b

返回int1 + 2 * int2 + 3 * int3 + 4 * int4

def slugging_percentage(total_bases,int5):
(total_bases,int5) - > float#因此,例如我需要第二个函数的第一个函数的乘积
返回给定覆盖的基数总数和bat(int5)处的总数
>>> slugging_percentage(20,9)
2.22


返回total_bases / int5

def on_base_percentage( h,bb,hbp,ab,sf):
(int,int,int,int,int) - > float
根据球返回基于命中的百分比,击球,击球和sacrfice命中
>>> 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#以及为此
返回基础上的加块击中给定的基础百分比和段击百分比
>>> on_base_plus_slugging(1.0,2.0)
3.0


return on_base_percentage + slugging_percentage
$ b $ def OPS_value(on_base_plus_slugging):
(float) - >字符串
根据基础加段塞分数范围返回基础加段塞流值
>>> OPS_value(0.8234)
B

如果on_base_plus_slugging> 0.9000:
返回A
elif on_base_plus_slugging> 0.7667:
返回B
elif on_base_plus_slugging> 0.7000:
return C
else on_base_plus_slugging <0.7000:
返回F
elif on_base_plus_slugging == 0.7000:
返回F


解决方案

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

 def some_function():
全局变量#这是一个全局变量
#将变量

另一种方式,我相信你正在寻找的是在一个函数内调用一个函数,看起来像这样:

  def function_1(#some变量):
#Stuff可以在这里
nu m = function2(5)
#Stuff可能在这里

def function_2(a_number):
a_number = a_number * 2
返回a_number

这将使变量 num = 5 * 2。 p>

我希望这有助于。


# 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"

解决方案

Save the variable as global is one way.

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

The other way, which is what I believe you are looking for is to call a function inside of a function. This would look like this:

 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

This will make the variable num = 5*2.

I hope this helps.

这篇关于使用函数的输出作为另一个函数python中的输入来编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆