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

VSCode中Python函数返回值无法输出到终端的问题

问题解决:Python函数无输出的原因及修复

你的代码逻辑本身是对的,函数get_destination_index确实能返回目标地点的索引,但脚本文件里运行Python代码时,函数的返回值不会自动显示在终端——只有主动使用print()函数,才能把结果输出到终端。

修复方法

修改最后一行的函数调用,用print()包裹起来:

destinations = ["Paris, France", "Shanghai, China", "Los Angeles, USA", "São Paulo, Brazil", "Cairo, Egypt"]
test_traveler = ['Erin Wilkes', 'Shanghai, China', ['historical site', 'art']]

def get_destination_index(destination):
    destination_index = destinations.index(destination)
    return destination_index

# 用print输出函数返回值
print(get_destination_index("Los Angeles, USA"))

运行修改后的代码,终端就会输出2(因为Python列表索引从0开始,"Los Angeles, USA"是列表的第3个元素,对应索引2)。

你也可以先把返回值存入变量,再打印:

la_index = get_destination_index("Los Angeles, USA")
print(la_index)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 04:31:29