运行代码时,我似乎无法获得显示区域功能的主要功能。显示半径功能。我有面积函数使用半径函数中的变量。
<div class="col-md-6" align="right" style="margin-top: 45px">
<input type="text" id="BirthDate" name="BirthDate" required class="form-control" placeholder="تاریخ تولد">
@Html.ValidationMessageFor(model => model.BirthDate, "", new { @class = "text-danger" })
</div>
答案 0 :(得分:0)
好问题。
如果要使用半径函数中的变量d,则必须首先从半径函数中返回它。然后在调用函数时“捕获”它。 这使您可以在其他上下文中使用它。见下文:
def radius(x, y, a, b):
d = math.sqrt((x - a)**2 + (y - b)**2)
print('The distance between the center points: ', d)
return d
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 = radius(x, y, a, b)
area(d)
答案 1 :(得分:0)
作为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
调用中的area(d)
未定义,因为未在该特定范围内声明d
。