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

Python代码无报错却无返回值,已调用全部方法的问题咨询

问题原因与解决方法

你的代码逻辑完全正确,所有函数都正常计算并返回了结果,也把结果赋值给了对应的变量,但Python不会自动输出变量的值——你只是将计算结果存在变量里,却没有明确让程序把这些值打印出来,所以运行后看不到任何输出,也不会触发报错。

修改后的代码

只需在每个变量赋值后添加print()语句,就能看到预期的数值:

train_mass = 22680
train_acceleration = 10
bomb_mass = 1

def f_to_c(f_temp):
  c_temp = (f_temp - 32) * 5/9
  return c_temp
f100_in_celsius = f_to_c(100)
print(f100_in_celsius)

def c_to_f(c_temp):
  f_temp = c_temp * (9/5) +32
  return f_temp
c0_in_fahrenheight = c_to_f(0)
print(c0_in_fahrenheight)

def get_force(mass, acceleration):
  return mass*acceleration
train_force = get_force(train_mass, train_acceleration)
print(train_force)

def get_energy(mass, c=3*10**8):
  return mass * c**2
bomb_energy = get_energy(bomb_mass)
print(bomb_energy)

简化写法

也可以在代码末尾统一打印所有变量,效果相同:

print(f100_in_celsius, c0_in_fahrenheight, train_force, bomb_energy)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 22:37:13