期望一个缩进的块

时间:2014-03-21 21:48:43

标签: python

我在这里得到了一个缩进的块,这是代码,谢谢你的帮助。

        #Given the menu the user will calculate the area of square, circle, rectangle
from math import pi
def main ():

    #declare and initialize variables

    #float radius = length = width = base = height = 0.0
    radius = length = width = base = height = 0.0

    #float areaCircle = areaRectangle = areaTriangle = 0.0
    areaCircle = areaRectangle = areaTriangle = 0.0

    #int menuChoice = 0
    menuChoice = 0

    #display intro

    while menuChoice != 4:
            #Display menu
            print("Geometry Calculator")
            print("1) Calculate the Area of a Circle")
            print("2) Calculate the Area of a Rectangle")
            print("3) Calculate the Area of a Triangle")
            print("4) Quit")

            #Prompt for menuChoice

            if menuChoice == 1:
                while radius < 0:
                    #prompt for radius
                    radius = eval(input("What is radius of circle: "))
                if radius < 0:
                    #display invalid
                    print("Invalid input. Cannot be a negative value.")
                    #calculate areaCircle
                    areaCircle = pi*r**2
                    #display areaCircle
                    print("The area of the circle is: ", areaCircle)

            elif menuChoice == 2:
                while length < 0:
                    #prompt for length
                    length = eval(input("What is the length of the rectangle: "))
                if length < 0:
                    #display invalid
                    print("Invalid input. Cannot be a negative value.")
                while width < 0:
                    #prompt for width
                    width = eval(input("What is the width of the rectangle: "))
                if width < 0:
                    #display invalid
                    print("Invalid input. Cannot be a negative value.")
                    #calculate areaRectangle
                    areaRectangle = length * width
                    #diplay areaRectangle
                    print("The area of the rectangle is: ", areaRectangle)

            elif menuChoice == 3:
                while base < 0:
                    #prompt for base
                    base = eval(input("What is the length of the base of the triangle:"))
                if base < 0:
                   #display invalid
                    print("Invalid input. Cannot be a negative value.")
                while height < 0:
                    #prompt for height
                    height = eval(input("What is height of triangle"))
                if height < 0:
                    #display invalid
                    print("Invalid input. Cannot be a negative value.")
                    #calculate areaTriangle
                    areaTriangle = 1/2 * base * height
                    #display areaTriangle
                    print("The area of the triangle is: ", areaTriangle)

            elif menuChoice == 4:
                #display exit message

            else:
                #display invalid
                print("You must choose a number between 1-4 from the menu")

错误会在其他地方弹出。我一次尝试缩进一个,可能是一些小的,我忽略了编程的第三周。

2 个答案:

答案 0 :(得分:4)

最终elif块需要某种占位符。您可以使用标准Python非操作pass

elif menuChoice == 4:
    #display exit message
    pass

我假设这最终会被其他一些代码所取代,所以如果你继续工作,问题本身就会解决。如果您计划在此块中放置任何内容,请完全省略它。不需要任何无效的条件分支。

答案 1 :(得分:3)

这是最后一行(#display退出消息)。添加正确缩进的pass语句,直到您知道该怎么做。你需要一个实际的python语句,而不是注释。