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

2024-04-14

# 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

我相信您正在寻找的另一种方法是在函数内部调用函数。这看起来像这样:

 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.

我希望这有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用函数的输出作为另一个函数的输入 python 编码新手 的相关文章

  • mod_wsgi 下的 psp(python 服务器页面)代码?

    有没有办法在 apache mod wsgi 下运行 psp python 服务器页面 代码 虽然我们正在转向基于 wsgi 的新框架 但我们仍然有一些用 psp 编写的遗留代码 这些代码在 mod python 下运行 我们希望能够在托管
  • 在 Python 3.5 64 位上通过 pip 安装 OpenCV

    我尝试安装 OpenCV 但找不到任何合适的 pip 软件包 我决定上网查找有关如何安装它的官方文档 并发现this https opencv python tutroals readthedocs io en latest py tuto
  • Google PubSub 在阻止和等待消息时没有标准输出

    我正在使用这个问题底部的 Python 代码监听 Google PubSub 消息 它实际上是来自 Google 的异步拉取示例 我运行我的程序并输出到文件 python my script py tee log txt 如果我在接收消息时
  • python 线程是如何工作的?

    我想知道 python 线程是并发运行还是并行运行 例如 如果我有两个任务并在两个线程中运行它们 它们是同时运行还是计划同时运行 我知道GIL并且线程仅使用一个 CPU 核心 这是一个复杂的问题 需要大量解释 我将坚持使用 CPython
  • Python-从Excel文件读取时间时未获得正确的日期时间

    我有一个 Excel 文件 其中有 3 列作为日期时间或日期或时间字段 我正在通过阅读它xlrd包裹 我有时间milliseconds我想当我尝试将其转换回日期时间时我得到了错误的结果 我尝试将文件转换为csv以及 这也没有帮助 我得到了我
  • pyqt5调用url时处于无响应状态

    我刚刚在 pyqt 中实现了登录 但在调用 url 和获取响应之间 qt 窗口显示无响应状态 谁能建议我如何避免不响应状态 code class Login QDialog def init self height width parent
  • 使用另一个索引数组正确索引多维 Numpy 数组

    我正在尝试索引多维数组P与另一个数组indices 它指定我想要沿最后一个轴的哪个元素 如下所示 import numpy as np M N 20 10 P np random rand M N 2 9 index into the la
  • 这是在 python 中美白图像的正确方法吗?

    我在尝试着zero center and whiten CIFAR10数据集 但我得到的结果看起来像随机噪声 Cifar10数据集包含60 000尺寸的彩色图像32x32 训练集包含50 000和测试集包含10 000分别是图像 以下代码片
  • TensorFlow 未编译为使用 SSE(等)指令,但这些指令是可用的

    我第一次使用一些示例代码运行 TensorFlow 运行代码时我收到以下警告 有谁知道为什么会发生这种情况以及如何解决它 2017 03 31 02 12 59 346109 W c tf jenkins home workspace re
  • C++ 中的可变参数函数声明中省略了逗号

    我习惯于这样声明可变参数函数 int f int n 读书时C 编程语言我发现书中的声明省略了逗号 int f int n the comma has been omitted 这个语法似乎是 C 特定的 因为当我尝试使用 C 编译器编译它
  • 使用 PyQt4 在 QWidget 上进行 eventFilter

    我有一个 QMainWindow 其中包含DrawingPointsWidget 该小部件随机绘制红点 我通过使用以下命令为 MouseHovering 事件安装事件过滤器 在 QMainWindow 的状态栏中显示鼠标坐标self ins
  • UserDict 类的优点?

    使用有什么好处UserDict class 我的意思是 我真正得到的不是 class MyClass object def init self self a 0 self b 0 m MyClass m a 5 m b 7 我将写下以下内容
  • Pandas 多索引数据框删除行

    我的多索引 DF 如下 tuples list zip a a b b c d c d index pd MultiIndex from tuples tuples names i1 i2 df pd DataFrame 5 6 7 8 i
  • 如何在 Ubuntu 上通过 pip 安装 python3 版本的软件包?

    我两者都有python2 7 and python3 2安装在Ubuntu 12 04 符号链接python链接到python2 7 当我输入 sudo pip install package name 它将默认安装python2的版本pa
  • 如何使用 python / pywinusb 将 hid 数据发送到设备?

    我正在尝试使用 pywinusb 将输出报告发送到 pic18f4550 该设备可以接收数据 我已经使用 C 应用程序对其进行了测试 效果很好 另外 我可以使用 pywinusb 从设备读取数据 但我在尝试发送数据时遇到问题 这是我正在运行
  • 抑制来自 python pandas 描述的名称 dtype

    可以说我有 r pd DataFrame A 1 B pd Series 1 index list range 4 dtype float32 And r B describe mean std min max 给出输出 mean 1 0
  • numpy 沿第一个轴添加

    我想通过简单地沿第一个轴执行相同的加法来添加两个具有不同维度的数组 非矢量化解决方案 x np array 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 shape 4 3 2 y np a
  • Python Shutil.copy 如果我有重复文件,它会复制到新位置吗

    我正在与shutil copypython 中的方法 我找到了下面列出的定义 def copyFile src dest try shutil copy src dest eg src and dest are the same file
  • Mac OS X 上的 Python 框架和非框架构建之间的差异

    Question Mac OS X 上的 Python 框架构建和非框架构建 即标准 UNIX 构建 之间有什么区别 另外 各自的优点和缺点是什么 初步研究 以下是我在发布此问题之前找到的信息 Pythonmac SIG Why is Fr
  • 将下载的字体添加到 Tkinter

    我想下载一个开源字体并在我的 Python Tkinter 程序中使用它 如何告诉 Tkinter 从目录导入字体或将字体放在与程序相同的文件夹中 Note 我已经寻找答案一段时间了 甚至阅读了 Tkinter 的 API 参考 了解我能找

随机推荐