应为缩进b

2024-09-30 22:28:22 发布

您现在位置: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")

错误在else弹出。我试着一次缩进一个,可能是我在第三周忽略的一些小东西。在


Tags: oftheinputbaseisdisplaywidthlength
2条回答

最后一个elif块需要某种占位符。您可以使用标准的Python non-op,pass

elif menuChoice == 4:
    #display exit message
    pass

我假设这个代码最终会被其他代码取代,所以如果你继续工作,问题就会自行解决。如果你不打算把任何东西放在这个区块里,那就把它完全忽略掉。没有必要使用一个什么都不做的条件分支。在

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

相关问题 更多 >