![](/img/trans.png)
[英]How can I take an inut list from user in one function, and transfer the tuple data to another function?
[英]How can I transfer the value in one function to another function?
我需要將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()
您可以考慮在計算功能之外索取值:
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
還有其他更高級的選項,但這應該可以幫助您入門。 如果您有興趣,我可以在此答案中添加其他一些選項。
讓我們看一下函數的外觀。
我可以編寫一些名為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)
您可以從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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.