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

Python TypeError求助:调用solve()时缺少必需位置参数'df'

解决TypeError: solve() missing 1 required positional argument: 'df'问题

问题代码

用户编写的Python代码如下:

import pandas as pd
def solve(df):
print(df.loc[0:2, 'Total':'Payment'])

df = pd.read_csv('supermarket1.csv', sep=';')
solve(df)

报错信息

运行后出现以下错误:

Traceback (most recent call last):
  File "__tester__.python3", line 37, in <module>
    solve()
TypeError: solve() missing 1 required positional argument: 'df'

排查与解决

  1. 核心问题:函数调用未传参
    错误提示明确显示,执行代码时存在直接调用solve()的操作,没有传入必填的df参数。请检查测试环境中的实际运行代码,确保调用语句为solve(df),将读取到的DataFrame对象传递给函数。

  2. 额外修复:函数体缩进错误
    原代码中print语句未缩进,不属于solve函数的函数体,会引发语法错误。修正缩进后,函数才能正确执行内部逻辑。

修正后的完整代码

import pandas as pd

def solve(df):
    # 缩进print语句,确保属于函数体
    print(df.loc[0:2, 'Total':'Payment'])

df = pd.read_csv('supermarket1.csv', sep=';')
# 确保调用时传入df参数
solve(df)

内容的提问来源于stack exchange,提问作者Beatriz Gervásio

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 08:44:54