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

为什么计算圆面积周长的Python函数运行无报错但无输出?

问题原因

仅定义函数不会触发内部逻辑执行,你没有编写circle函数的调用语句,所以代码运行后没有任何输出,也不会抛出错误。

修复方法

  • 直接在代码末尾添加函数调用语句,传入指定的半径值即可执行内部逻辑,示例如下:
def circle(radius):
    pi = 3.141592
    a = pi * radius **2
    c = 2 * pi * radius
    print(f'Area is {a} and the circumference is {c}')

# 新增函数调用,此处以半径为3为例
circle(3)

运行上述代码后会输出:Area is 28.274328 and the circumference is 18.849552

  • 如果需要自定义输入半径,可以增加用户输入读取逻辑:
def circle(radius):
    pi = 3.141592
    a = pi * radius **2
    c = 2 * pi * radius
    print(f'Area is {a} and the circumference is {c}')

# 读取用户输入的半径值
input_r = float(input("请输入圆的半径:"))
circle(input_r)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 13:36:02