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

为何VSCode终端运行足球Twitter bot的Python函数无预期打印结果

问题原因

  • 函数调用后没有执行打印操作:你编写的NewTweet函数会将拼接好的推文内容作为返回值返回,但直接写NewTweet ('Bruno', 'shot', True)只是调用了函数、得到了返回结果,没有把结果输出到终端,自然看不到内容。
  • 传入的球员参数不符合预期:你调用函数时传的player参数值是'Bruno',不是你预期的'Bruno Fernandes',就算打印结果也不会出现完整的球员全名。
  • 预期文本和代码定义不一致:你想要的进球提示是GOOOALLL,但代码里写的是GOOOOAAAAALLLLL,输出会严格匹配代码内的字符串内容。

解决方法

修改代码如下即可得到预期输出:

def NewTweet(player, action, success):
    tweet = player + ' plays a ' + action + '.'

    if success:
        if action == 'pass':
            tweet += ' The team keeps possession'
        elif action == 'shot':
            # 调整为你想要的进球提示文本
            tweet += ' GOOOALLL'
    else:
        if action == 'pass':
            tweet += ' The pass is unsuccessful'
        elif action == 'shot':
            tweet += ' The shot goes wide'

    return tweet

# 传入完整球员名,同时用print函数把返回结果输出到终端
print(NewTweet('Bruno Fernandes', 'shot', True))

修改后运行代码,终端就会输出你预期的Bruno Fernandes plays a shot. GOOOALLL结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 23:48:04