我需要将calcRectArea中的宽度值传递给calcTriArea,这样我就可以计算山墙区域,而无需再次询问宽度。我对Python和一般编程都很陌生,所以如果这是一个愚蠢的问题,请原谅我。
def main():
'''
main adds rectangular area and triangular area to compute the total area
'''
rectarea=0
rectarea=calcRectArea(rectarea)
print("Rectangular area is now",rectarea)
triarea=0
triarea=calcTriArea(triarea)
print("Triangular area is now",triarea)
totalarea=triarea+rectarea
print("The total area of the first house is",totalarea)
print("For the second house: ")
rectarea2=0
rectarea2=calcRectArea(rectarea2)
print("Rectangular area of second house is now",rectarea2)
triarea2=0
triarea2=calcTriArea(triarea2)
print("Triangular area of the second house is now",triarea2)
totalarea2=triarea2+rectarea2
print("The total area of the second house is",totalarea2)
totalbothhouses=totalarea+totalarea2
print("The combined area of both houses is",totalbothhouses)
def calcRectArea(RectAreaTotal):
'''
calcRectArea prompts the user to enter width, height, and length, computes the
front and side areas, and adds them to compute rectangular area
'''
width=input("Enter the width: ")
width=int(width)
height=input("Enter the height: ")
height=int(height)
length=input("Enter the length: ")
length=int(length)
front=(width*height)
side=(length*height)
RectAreaTotal=(front*2)+(side*2)
return RectAreaTotal
def calcTriArea(totalgablearea):
'''
calcTriArea has the user enter the gable height and computes triangular area
'''
gableheight=input("Enter the gable height: ")
gableheight=int(gableheight)
totalgablearea=(gableheight)
return totalgablearea
main()
答案 0 :(得分:1)
您可以考虑询问计算函数之外的值:
def get_dimensions():
height = int(input("Enter the height: "))
width = int(input("Enter the width: "))
length = int(input("Enter the length: "))
height, width, length = get_dimensions()
# go on to pass the values to your functions
还有其他更高级的选项,但这应该可以帮助您入门。如果您有兴趣,我可以为此答案添加一些其他选项。
答案 1 :(得分:1)
让我们看一下函数的样子。
我可以编写一些名为foo的任意函数,使其具有单个输入和输出:
def foo(a):
return a
f = foo(1) # f == 1
我也可以用4输入和4输出写它:
def foo(a, b, c, d):
return a, b, c, d
f, g, h, i = foo(1, 2, 3, 4) # f = 1, g = 2, h = 3, i = 4
函数定义允许您指定所需的任意数量的输入。您还会注意到在python中,您可以返回多个值!在您的示例中,您可以简单地更改当前函数以接受额外值。
def calcTriArea(totalgablearea):
变为
def calcTriArea(totalgablearea, calcRectArea):
现在您需要在rectArea中更改返回值,以返回额外的值。
return RectAreaTotal, width
现在您可以在triArea函数中访问calcRectArea
的宽度了!你现在只需将它传递给函数就像这样:
rectarea, width=calcRectArea(rectarea)
print("Rectangular area is now",rectarea)
triarea=0
triarea=calcTriArea(triarea, width)
print("Triangular area is now",triarea)
答案 2 :(得分:0)
您可以从main获取值并将其传递给两个函数calcRectArea
和calcTriArea
检查包括main
在内的函数的定义以实现更改:
def main():
'''
main adds rectangular area and triangular area to compute the total area
'''
rectarea=0
width=input("Enter the width: ")
width=int(width)
rectarea=calcRectArea(rectarea,width)
print("Rectangular area is now",rectarea)
triarea=0
triarea=calcTriArea(triarea,width)
print("Triangular area is now",triarea)
totalarea=triarea+rectarea
print("The total area of the first house is",totalarea)
print("For the second house: ")
rectarea2=0
width=input("Enter the width: ")
width=int(width)
rectarea2=calcRectArea(rectarea2, width)
print("Rectangular area of second house is now",rectarea2)
triarea2=0
triarea2=calcTriArea(triarea2, width)
print("Triangular area of the second house is now",triarea2)
totalarea2=triarea2+rectarea2
print("The total area of the second house is",totalarea2)
totalbothhouses=totalarea+totalarea2
print("The combined area of both houses is",totalbothhouses)
def calcRectArea(RectAreaTotal, width):
'''
calcRectArea prompts the user to enter width, height, and length, computes the
front and side areas, and adds them to compute rectangular area
'''
height=input("Enter the height: ")
height=int(height)
length=input("Enter the length: ")
length=int(length)
front=(width*height)
side=(length*height)
RectAreaTotal=(front*2)+(side*2)
return RectAreaTotal,width
def calcTriArea(totalgablearea, width):
'''
calcTriArea has the user enter the gable height and computes triangular area
'''
gableheight=input("Enter the gable height: ")
gableheight=int(gableheight)
totalgablearea=(gableheight)
return totalgablearea