You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将数组元素代入y=3x+2计算并封装为Ecuacion函数打印结果

实现方案

以下Python代码完全符合要求,已将运算逻辑封装为指定的Ecuacion函数,遍历数组全部元素代入公式Y = 3x + 2计算后,会打印所有运算结果:

# 实验需要用到的数组A
A = [3, 3, 20, 8, -2, 1, 3, -7, 2, 1, 3, -5, 13]

def Ecuacion():
    res = []
    for x in A:
        # 代入公式计算
        y = 3 * x + 2
        res.append(y)
    # 打印所有计算结果
    print("全部计算结果:", res)
    return res

# 执行函数
Ecuacion()

运行输出

全部计算结果: [11, 11, 62, 26, -4, 5, 11, -19, 8, 5, 11, -13, 41]

如果需要函数支持传入任意数组计算,可以调整为带参数的版本:

def Ecuacion(arr):
    res = [3 * x + 2 for x in arr]
    print("全部计算结果:", res)
    return res

# 调用时传入数组A即可
A = [3, 3, 20, 8, -2, 1, 3, -7, 2, 1, 3, -5, 13]
Ecuacion(A)

内容的提问来源于stack exchange,提问作者SaryxEze

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 20:39:03