2017-01-14 2 views
1

"다른 모양의 영역을 찾으시겠습니까?"에 "아니오"를 입력 할 때마다 그것은 저에게 질문을 다시 묻습니다. 여기 내 코드는 다음과 같습니다 -파이썬 코드가 두 번 실행되는 이유는 무엇입니까?

def calculate_area(): 
    print "Welcome to the area calculator" 
    user= raw_input("Enter the shape you would like to calculate the area of:").lower() 

    def restart(): 
    answer= raw_input("Would you like to find the area of another shape?('yes' or 'no')") 
    if answer=="yes": 
     calculate_area() 
    def rerun(): 
    restart() 


    if user== "rectangle": 
    def calculate_rectangle(): 
     rect1= int(raw_input("Enter the length of the first side:")) 
     rect2= int(raw_input("Enter the length of the second side:")) 
     print "The area is:",float(rect1*rect2) 
    calculate_rectangle() 
    rerun() 

    elif user== "square": 
    def calculate_square(): 
     square=int(raw_input("Enter the length of the side:")) 
     print "The area is:",float(square**2) 
    calculate_square() 
    rerun() 


    elif user== "triangle": 
    def calculate_triangle(): 
     triangle=int(raw_input("Enter the length of the base:")) 
     triangle2=int(raw_input("Enter the height of the triangle:")) 
     print "The area is:", float((0.5*triangle)*triangle2) 
    calculate_triangle() 
    rerun() 


    elif user== "trapezoid": 
    def calculate_trap(): 
     trapezoid=int(raw_input("Enter the length of base 1:")) 
     trapezoid2=int(raw_input("Enter the length of base 2:")) 
     trapezoid3=int(raw_input("Enter the height:")) 
     print "The area is:", (float(trapezoid+trapezoid2)/2*float(trapezoid3)) 
    calculate_trap() 
    rerun() 

    elif user== "circle": 
    def calculate_circle(): 
     circle=int(raw_input("Enter the radius:")) 
     print "The area is:", (float((circle**2)*3.14)) 
    calculate_circle() 
    rerun() 

    elif user== "rhombus": 
    def calculate_rhombus(): 
     rhombus1=int(raw_input("Enter the length of diagonal 1:")) 
     rhombus2=int(raw_input("Enter the length of diagonal 2:")) 
     print "The area is:", (float((rhombus1*rhombus2)/2)) 
    calculate_rhombus() 
    rerun()  

    else: 
    print "Shape not recognized" 
    rerun() 

이 코드는 "데프 다시 시작"아래에 내가 입력 할 때마다 두 번 실행 "아니오." 왜 이런 일이 일어나는 걸까요?

+4

() '함수의 끝에서 ... – mguijarr

+0

당신이 디버거에서 그것을 실행 시도하셨습니까'재방송이 있기 때문에, "다른 모양의 영역을 찾으시겠습니까?"줄에 중단 점이 있습니까? 두 번에 스택 추적을 확인하면 대답을 알 수 있다고 생각합니다. – alf

+0

마지막'''rerun()'''은 마지막''else''' 블록 아래 들여 쓰이지 않습니다. –

답변

0

마지막으로 다시 실행은() else 문 안에 있어야한다 :

print ("Welcome to the area calculator") 
    user = raw_input("Enter the shape you would like to calculate the area of:").lower() 

    def restart(): 
    answer = raw_input("Would you like to find the area of another shape?('yes' or 'no')") 
    if answer == "yes": 
     calculate_area() 
    def rerun(): 
    restart() 


    if user == "rectangle": 
    def calculate_rectangle(): 
     rect1 = int(raw_input("Enter the length of the first side:")) 
     rect2 = int(raw_input("Enter the length of the second side:")) 
     print "The area is: ",float(rect1 * rect2) 
    calculate_rectangle() 
    rerun() 

    elif user == "square": 
    def calculate_square(): 
     square=int(raw_input("Enter the length of the side:")) 
     print "The area is: ",float(square ** 2) 
    calculate_square() 
    rerun() 


    elif user == "triangle": 
    def calculate_triangle(): 
     triangle = int(raw_input("Enter the length of the base:")) 
     triangle2 = int(raw_input("Enter the height of the triangle:")) 
     print "The area is: ", float((0.5 * triangle) * triangle2) 
    calculate_triangle() 
    rerun() 


    elif user == "trapezoid": 
    def calculate_trap(): 
     trapezoid = int(raw_input("Enter the length of base 1:")) 
     trapezoid2 = int(raw_input("Enter the length of base 2:")) 
     trapezoid3 = int(raw_input("Enter the height:")) 
     print "The area is: ", (float(trapezoid + trapezoid2)/2 * float(trapezoid3)) 
    calculate_trap() 
    rerun() 

    elif user == "circle": 
    def calculate_circle(): 
     circle = int(raw_input("Enter the radius:")) 
     print "The area is: ", (float((circle ** 2)*3.14)) 
    calculate_circle() 
    rerun() 

    elif user == "rhombus": 
    def calculate_rhombus(): 
     rhombus1 = int(raw_input("Enter the length of diagonal 1:")) 
     rhombus2 = int(raw_input("Enter the length of diagonal 2:")) 
     print "The area is: ", (float((rhombus1 * rhombus2)/2)) 
    calculate_rhombus() 
    rerun()  

    else: 
    print "Shape not recognized" 
    rerun() 
+0

감사합니다! 저기서 보지 못했어요. 다른 사람을 끼워 넣고, 일했습니다. –

1

방법의 마지막에 rerun()이 있습니다.

발췌문 : 당신은 모든 질문에 대해 두 번 rerun() 전화

...removed... 
    else: 
    print "Shape not recognized" 
    rerun() 
3

:

if user== "rectangle": 
    # ... 
    rerun() 

elif user== "square": 
    # ... 
    rerun() 

# all other elif branches each have rerun() 

else: 
    print "Shape not recognized" 
rerun() 

현재 재귀에 의존하고 있지만, 재귀 함수 반환; "No"을 입력하면 은 rerun()으로 돌아가며 호출 된 지점까지 컨트롤을 반환하므로 if ... elif ... 브랜치 중 하나에서 컨트롤을 반환합니다. 그리고 이후 번 지점에 rerun()으로 다시 전화하십시오.

처음에는 재귀를 사용해서는 안됩니다. 대신에 무한 루프를 사용

print "Welcome to the area calculator" 

while True: 
    user = raw_input("Enter the shape you would like to calculate the area of:").lower() 
    # execute their choice 
    if user== "rectangle": 
     # ... 
    # etc. 

    else: 
     print "Shape not recognized" 

    answer = raw_input("Would you like to find the area of another shape?('yes' or 'no')") 
    if answer != "yes": 
     break 

끝에있는 breakwhile True 루프를 종료합니다. yes이 입력되면 while 루프가 맨 위부터 계속되고 전체 블록을 다시 실행합니다.

관련 문제