### The formulas for the area and perimeter.
def area(a, b, c):
# calculate the sides
s = (a + b + c) / 2
# calculate the area
areaValue = (s*(s-a)*(s-b)*(s-c)) ** 0.5
# returning the output
# Calculate the perimeter
perimValue = a + b + c
# returning the output.
return areaValue,perimValue
areaV, perimeterV = area(a, b, c)
### The main function for the prompts and output.
def main():
# The prompts.
a = int(input('Enter first side: '))
b = int(input('Enter second side: '))
c = int(input('Enter third side: '))
# The output statements.
print ("The area is:", format(areaV(a, b, c),',.1f'),"and the perimeter is:", format(perimeterV(a, b, c), ',.1f'))
### Calling msin
main()
我试图从区域函数返回两个值,但是当我尝试这样做时,我收到一条错误消息,指出当我尝试调用该函数时 a b 和 c 未定义。
注意:我的老师告诉我们面积和周长只需要用 1 个函数来计算。他们不能分开。
有什么方法可以阻止该错误发生吗?
最佳答案
你需要输入
areaV, perimeterV = area(a, b, c)
在用户输入后的 main 中。因为a,b,c
是在main函数范围内定义的
应该是这样的:
def main():
# The prompts.
a = int(input('Enter first side: '))
b = int(input('Enter second side: '))
c = int(input('Enter third side: '))
areaV, perimeterV = area(a, b, c)
# The output statements.
print ("The area is:", format(areaV,',.1f'),"and the perimeter is:", format(perimeterV, ',.1f'))
关于python - 返回两个带有函数参数的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28550786/