0
import math
def maths():
  shape=input("Choose a shape:Square, Circle or Triangle ")
  if shape == "Triangle" or "triangle":
    triangleb = int(input("How many cm's is the base of your triangle? "))
    triangleh = int(input("How many cm's is the height of your triangle? "))
    t_area = triangleb*triangleh / 2
    print("The area of your triangle is",t_area)
    print("")
    maths()

  elif shape == "Circle" or "circle":
    circle=int(input("What is the radius of your circle? "))
    c_area=math.pi*circle*circle
    c_arearound=round(c_area ,2)
    print("The area of your circle is",c_arearound,"(rounded to 2 decimal places)")
    print("")
    maths()

 elif shape == "square" or "Square":
    square=int(input("How many cm is one side of your square "))
    s_area=square*square
    print("The area of your square is",s_area)
    print("")
    maths()
 else:
    print("invalid answer")
    maths()

maths()   

Each element of the IF statement works exactly correctly when they start with 'if. The problem is that if I input 'circle' the code for triangle will appear. For example I would type circle and it would say 'how many cm's is the base of your triangle?'. This happens for whatever I type whether its circle or square it will print the code for triangle. If I switch the 'if' statement for the circle instead of triangle the exact same happens. I could input triangle or square and it would run the code for circle only, whatever the input is. Why??

  • Two things you might want to try. 1. It looks like your indentation is not consistent (Python has significant white space) 2. Delete the generated `__pycache__` directory or the generated `*.pyc` files (not the `*.py` source files) – Evan Porter Sep 11 '17 at 19:32
  • where can I find the _pycache_ directory or the *.pyc files?? – user8549523 Sep 11 '17 at 19:36
  • 1
    Ugh. What makes so many people think that `if stuff = a or b or c` will actually compare `stuff` against `a`, `b` and `c` and return True if any of these comparisons succeed? _You have to do the comparisons yourself!!_ Also, you could just check `if shape.lower() == 'triangle'` and so on. This is much simpler. – ForceBru Sep 11 '17 at 19:51

0 Answers0