首页 > 解决方案 > 试图让main函数在python中显示一个函数

问题描述

运行代码时,我似乎无法获得显示区域功能的主要功能。它显示半径函数。我让面积函数使用半径函数中的变量。

def radius(x, y, a, b):
    d = math.sqrt((x - a)**2 + (y - b)**2)
    print('The distance between the center points: ', d)


def area(d):
    pi = math.pi
    ar = pi * d**2
    print('The area of the circle: ', ar)


def main():
    print('Enter first coordinates:')
    x = int(input('Enter X: '))
    y = int(input('Enter Y: '))
    print('Enter second coordinates: ')
    a = int(input('Enter X: '))
    b = int(input('Enter Y: '))

    radius(x, y, a, b)
    area(d)

main()

标签: pythonfunctionmain

解决方案


作为 Python 世界中一个有抱负的年轻人,你应该尝试习惯使用return语句。长大后你会发现你并不总是需要它们,但让我们稍后再说吧。

这是您更正的代码:

def radius(x, y, a, b):
    r = math.sqrt((x - a)**2 + (y - b)**2) / 2  # you call the function radius but calculate the diameter -> confusing!
    return r


def area(d):
    ar = math.pi * d**2
    return ar


def main():
    print('Enter first coordinates:')
    x = int(input('Enter X: '))
    y = int(input('Enter Y: '))
    print('Enter second coordinates: ')
    a = int(input('Enter X: '))
    b = int(input('Enter Y: '))

    D = 2 * radius(x, y, a, b)
    print('The distance between the center points: ', D)
    A = area(D)
    print('The area of the circle: ', A)
    return

main()

原始问题的问题在于未定义don call,因为未在该特定范围内声明。area(d)d


推荐阅读