为什么我的python代码运行两次?

时间:2017-01-14 17:19:33

标签: python

每当我输入“no”代表“你想找到另一个形状的区域吗?”它再次问我这个问题。这是我的代码: -

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”时都会运行两次。为什么会这样?

3 个答案:

答案 0 :(得分:3)

您为每个问题致电rerun()两次:

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

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

# all other elif branches each have rerun()

else:
    print "Shape not recognized"
rerun()

你在这里依赖递归,但是递归函数 return ;当您输入"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循环从顶部继续,重新运行整个块。

答案 1 :(得分:1)

您的方法结束时有一个rerun()

段:

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

答案 2 :(得分: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()