2014-09-23 5 views
1

너비를 다시 묻지 않고 게이블 영역을 계산할 수 있도록 calcRectArea의 너비 값을 calcTriArea로 전송해야합니다. 저는 파이썬과 프로그래밍 전반에 새로운 것이므로 어리석은 질문이라면 저를 용서하십시오. 다른 훨씬 더 고급 옵션이 있습니다하나의 함수에서 다른 함수로 값을 전송하려면 어떻게해야합니까?

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 

,하지만 당신이 시작할 수 있어야합니다

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

y는이 방법으로 당신이 어떤 함수에서 제안 – Tushar

+0

감사하는 입력 할 수있을 잡아 줄께 ... 당신이 당신의 주요 기능에 입력을 trake 해달라고, 그러나 나는 결국 변수들을 세계화시켰다. –

+0

@JoeyCartella NO NO NO! 그러지 마. ** 언제든지 가능하면 전역을 피하십시오. –

답변

1

당신은 당신의 계산 기능 이외의 값을 요청하는 것이 좋습니다. 관심이 있으시면이 답변에 몇 가지 다른 옵션을 추가 할 수 있습니다.

0

당신은 주에서 값을 가져 와서 두 함수에 전달할 수, 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 
1

살펴볼 수 있습니다 무엇을 함수가 생겼다.

I는 하나의 입력과 출력을 갖도록라는 foo는 어떤 임의의 함수를 작성할 수

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 

함수 :

def foo(a): 
    return a 

f = foo(1) # f == 1 

I는 4 개 입력 및 4 개 출력을 기록 할 수 정의를 사용하면 원하는 수의 입력을 지정할 수 있습니다. 또한 파이썬에서는 여러 값을 반환 할 수 있습니다. 예를 들어, 현재 함수를 변경하여 추가 값을 받아 들일 수 있습니다.

def calcTriArea(totalgablearea): 

def calcTriArea(totalgablearea, calcRectArea): 

지금 당신이 여분의 값을 반환하는 rectArea에 반환 한 Statment을 변경해야된다. 당신의 triArea 기능에의

return RectAreaTotal, width 

지금 당신의가 액세스 할 수 calcRectArea의 폭! 이제 너무 같은 함수에 전달해야합니다

rectarea, width=calcRectArea(rectarea) 
print("Rectangular area is now",rectarea) 

triarea=0 
triarea=calcTriArea(triarea, width) 
print("Triangular area is now",triarea) 
관련 문제