1

I'm getting an Expected an indented block here's the code, thank you for any help.

        #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")

The error pops up at else. I've tried indenting one at a time, probably something small i'm overlooking third week into programming.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

2 Answers2

3

You need some sort of placeholder for the final elif block. You may use the standard Python non-op, pass:

elif menuChoice == 4:
    #display exit message
    pass

I'm assuming this will eventually be replaced by some other code, so the problem would have resolved itself had you continued working. If you are not planning on putting anything in this block, omit it entirely. There is no need for a conditional branch that does nothing.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • yea, I simply forgot to type something there, still very new to this. now i ran the code and it just goes into an infinite loop, do you have any idea? –  Mar 21 '14 at 22:01
  • @user3320792 Yes. This is because `menuChoice` starts out as 0, and all your logic for pausing and prompting the user happens under conditional blocks that will never be entered when `menuChoice` is 0. The script always goes into the else branch, and keeps printing `You must choose a number between 1-4` very rapidly in an endless loop. – Asad Saeeduddin Mar 21 '14 at 22:05
  • (I hope i'm not violating any rules by having other questions inside question)How could I fix it, i set menuChoice to a number within menu range and it does the same. I've only been coding for a couple of weeks, would really appreciate the help!! –  Mar 21 '14 at 22:13
  • @user3320792 It actually is somewhat against the rules to ask multiple run on questions (unless of course you have questions about the answer provided). What we can do is either continue this in chat, or you can ask a separate question about your new problem (which is encouraged). Please do try to look around at existing questions first though, and take a shot at debugging it carefully; even if you don't manage to fix it yourself, you'll have gained information that will help others help you. – Asad Saeeduddin Mar 21 '14 at 22:18
2

It's the last line (#display exit message). Add a correctly indented pass statement, until you know what to do here. You need an actual python statement here, not a comment.

Simon
  • 2,840
  • 2
  • 18
  • 26