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

注释print()语句导致Python代码运行报错的问题求助

Python开发中空print语句异常影响程序运行问题

问题现象

代码保留指定位置的空print()语句时,程序可正常运行;注释或删除该行后程序直接报错。
可正常运行的代码如下:

def max_pairwise_product(numbers):
    max_1 = max(numbers)
    numbers.remove(max_1)
    max_2 = max(numbers)
    return max_1 * max_2


if __name__ == '__main__':
    input_n = int(input())
    print()              # <- 注释/删除该行会导致代码报错
    input_numbers = [int(x) for x in input().split()]
    print(max_pairwise_product(input_numbers))

报错信息

注释或删除第10行空print()后,运行抛出如下错误:

Traceback (most recent call last):
  File "C:\Users\...\maximum_pairwise_product_fast.py", line 12, in <module>
    print(max_pairwise_product(input_numbers))
  File "C:\Users\...\maximum_pairwise_product_fast.py", line 2, in max_pairwise_product
    max_1 = max(numbers)
ValueError: max() arg is an empty sequence

Process finished with exit code 1

已测试的环境情况

  • 本地环境:PyCharm + Python 3.9,先后切换Python 3.8、3.10版本虚拟环境测试,均能复现该错误
  • 其他环境:同一份代码在Jupyter、Colab环境运行无任何异常
  • 本地环境状态:已正常使用数月,其余所有Python脚本运行均正常,从未出现过类似反常问题,暂无法定位问题根因。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.03 00:39:25