If statement inside an if statement?


#1

I’ve been expanding on the area calculator project adding more shapes and stuff and I want to add volume and parameter to each shape is there a way to do an if statement in an if statement so the user would pick volume, area or parameter and then if fx they chose area they would get the shape options in the project
fx.

if true
  then if
elif true
  then if
etc 

#2

Hi @williamburgdorffjens,

Yes, you can nest if, elif, and else blocks, using a pattern that is something like this or a variant of it …

<statements> # user chooses a type of shape
if <condition>: # shape a
  <statements> # user chooses volume, area, perimeter, or other
  if <condition>: # volume
    <statements>
  elif <condition>: # area
    <statements>
  else: # perimeter
    <statements>
elif <condition>: # shape b
  <statements> # user chooses volume, area, perimeter, or other
  if <condition>: # volume
    <statements>
  elif <condition>: # area
    <statements>
  else: # perimeter
    <statements>
elif <condition>: # shape c
  <statements> # user chooses volume, area, perimeter, or other
  if <condition>: # volume
    <statements>
  elif <condition>: # area
    <statements>
  else: # perimeter
    <statements>
else: # invalid shape chosen
    <statements> # explain the problem and exit

Include as many elif blocks as needed.

EDITED October 1, 2017 to add details to the code pattern, including comments


#3

Thanks I can’t quite get it to work when I try all the elif statements belong under the first if statement instead of the if statement inside the first one


#4

If you encounter problems with the nesting of the blocks or with other details, you can post your code for other users to inspect. Make sure it is formatted, so that we can see the indentation.


#5

Following is an example program that handles two different shapes, namely a square and a cube. It was written for Python 3, therefore it uses input instead of raw_input.

You can use this as a model for the structure of a larger program. Note that each outer conditional block processes a particular shape, and each inner conditional block processes the selected parameter for the selected shape. Add elif blocks to provide additional choices of shapes and parameters.

# ShapeCalculator.py
# Written for Python 3
# Outer conditional blocks handle the shapes
# Inner conditional blocks handle the parameters
print("The shape calculator is now live!")
hint = "Don't forget to include the correct units!\nExiting..."
option = input("Enter S for Square or C for Cube: ").upper()
if option == "S": # Square
    side = float(input("Enter the side length: "))
    option = input("Enter A for Area or P for Perimeter: ").upper()
    if option == "A": # Square Area
        area = float(side ** 2)
        print("{:0.4f}\n{:s}".format(area, hint))
    elif option == "P": # Square Perimeter
        perimeter = 4.0 * side
        print("{:0.4f}\n{:s}".format(perimeter, hint))
    else: # Invalid Square Parameter
        print("Garbage in, garbage out. Exiting ...")
elif option == "C": # Cube
    edge = float(input("Enter the edge length: "))
    option = input("Enter A for Surface Area or V for Volume: ").upper()
    if option == "A": # Cube Surface Area
        area = 6.0 * edge ** 2
        print("{:0.4f}\n{:s}".format(area, hint))
    elif option == "V": # Cube Volume
        volume = float(edge ** 3)
        print("{:0.4f}\n{:s}".format(volume, hint))
    else: # Invalid Cube Parameter
        print("Garbage in, garbage out. Exiting ...")
else: # Invalid Shape
   print("Garbage in, garbage out. Exiting ...")

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.