I'm writing a simple program to calculate the area of different shapes. The issue is when I include an OR statement, it directs to the wrong if statement. This is difficult to explain and I haven't been able to find the right words to get a meaningful result, so here I am. Here's the code.
import time
import math
import sys
def calc():
shape = input("Circle, Square/Rectangle, or Triangle?: ")
if shape.lower() == "circle":
rad = int(input("Enter the radius of the circle: "))
pi = float(math.pi)
area = float((rad*rad)*pi)
#rounded = round(area, 3)
print("The area of your circle is: ", area)
again()
elif shape.lower() == "square":
L = int(input("Enter Length: "))
H = int(input("Enter Height: "))
area = float(L*H)
print("The area is ", area)
again()
elif shape.lower() == "triangle":
base = int(input("Enter the base: "))
height = int(input("Enter the Height: "))
area = int((base*height)/2)
print("The area of your triangle is.. ", area)
again()
def again():
ag = input("Again?")
if ag == "y":
calc()
else:
sys.exit
calc()
I wanted to accept two values for the square. Either square or rectangle would be accepted by changing it to this. Also in the end, it runs the code at the bottom to ask the user if they have another shape.
elif shape.lower() == "square" or "rectangle":
L = int(input("Enter Length: "))
H = int(input("Enter Height: "))
area = float(L*H)
print("The area is ", area)
again()
def again():
ag = input("Again?")
if ag.lower == "y" or "yes":
calc()
elif ag.lower() == "n" or "no":
sys.exit
When I run this code in any other program, there is no issue accepting two values. With this program, when I type "triangle", it completely skips triangle and returns length/height prompt as if I typed square. If I remove the OR part for square, it has no issue going to triangle.
The same goes for the exit script. If I leave the OR in there, it completely skips the exit prompt. If I type Y or YES, it runs again. If I type N or NO, it runs again. Once again when I take out the OR part, it runs no problem.
I understand I could just create a separate IF/ELSE for rectangle, and the values are the same, but this is an issue I've run into before, and I'd like some help with solving it. Can anyone shed some light on this issue?