2017-01-14 67 views
1

每當我爲「你想找到另一個形狀的區域?」鍵入「否」時它再次問我這個問題。這裏是我的代碼: -爲什麼我的python代碼運行兩次?

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() 

此代碼是在「def restart」下,每當我輸入「no」時運行兩次。這是爲什麼發生?

+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()

段:

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

你叫rerun()兩次,每一個問題:

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

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

# all other elif branches each have rerun() 

else: 
    print "Shape not recognized" 
rerun() 

你是依靠遞歸這裏,但遞歸函數回報;當你輸入"No"時,restart()返回到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 

break末結束while True循環。如果輸入yes,則while循環從頂部繼續,重新運行整個塊。

相關問題