有人能指出这段代码中的错误吗?

时间:2017-04-07 04:14:44

标签: python

我是初学python程序员,试图执行这段代码,但我还不太了解def功能......有人能指出代码中的错误以及如何修复它们吗?

import math


def main():
    radius = get_radius()
    calculate = cal_volume()
    dis = display()

def get_radius():
    rad = float(input("Enter the radius :"))
    return rad

def cal_volume(radius):
    return 4/3*math.pi*radius**3

def display(cal_volume):
    print("The volume is :",cal_volume)

main()    

2 个答案:

答案 0 :(得分:1)

您没有在cal_volume()中为display()def main()提供参数。

应该是:

import math


def main():
    radius = get_radius()
    calculate = cal_volume(radius)
    dis = display(calculate)
    # print out the result?

def get_radius():
    rad = float(input("Enter the radius :"))
    return rad

def cal_volume(radius):
    return 4/3*math.pi*radius**3

def display(cal_volume):
    print("The volume is :",cal_volume)

main()

答案 1 :(得分:0)

试试这个:

import math

def main():
    radius = get_radius()
    calculate = cal_volume(radius)
    dis = display(calculate)

def get_radius():
    rad = float(input("Enter the radius :"))
    return rad

def cal_volume(radius):
    return 4/3*math.pi*radius**3

def display(cal_volume):
    print("The volume is :",cal_volume)

main()