python - 试图让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()
解决方案
作为 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()
原始问题的问题在于未定义d
on call,因为未在该特定范围内声明。area(d)
d
推荐阅读
- php - Laravel 8根据类别ID数据获取产品
- c# - 将 Json 数据转换为 datagridview C#
- ejabberd - 是否可以使用 ejabberd REST API 更新房间标题(和其他选项)?
- python - 如何通过python将文件从Sharepoint复制到本地文件夹
- bash - 带有模式和引号字符的 bash 循环
- vba - VBA Excel 无法更改绘图标题的布局
- javascript - 选中时 React Set Class Active
- python - 为数据框创建日期列
- state - 分层状态机 (HSM) 当前状态
- google-calendar-api - Google Calendar API,缺少事件通知