I've made a simple program that can calculate the area of various 2D shapes. I'd like your expereanced eyes to asses my code. Be as harsh as you want but please be specific and offer me ways in which I can improve my coding.
shape = ("triangle","square","rectangle","circle") # This is a tuple containing the string names of all the supported shapes so far.
constants = (3.14) # An object containing the value of pi. I will add more constants in future and turn this into a tuple.
start_up = input("Do you want to calculate a shape's area?: ") # Fairly self explainatory
if start_up != "yes" or "no": # This is to prevent the program failing if the user answers the start_up question
print("Sorry but I don't understand what your saying") # with an answer outside of the simple yes/no that it requires
start_up = input("Do you want to calculate a shape's area?: ") # re-iterates the start up question.
while start_up == "yes": # This is the condition that obviously contains the bulk of the program.
target_shape = input("Choose your shape: ") # Once again, self explainatory.
if target_shape == shape[0]: # Uses the shape tuple to bring forth the case for the first shape, the triangle.
h = float(input("give the height: ")) # These Two lines allow the user to input the required values to calculate the area of the triangle.
b = float(input("give the base length: ")) # They are using the float funtion to increase the accuracy of the answer.
area_triangle = h * 1/2 * b # The formula we all learned in school.
print("the area of the %s is %.2f" % (shape[0],area_triangle)) # This generates a format string which uses both the shape tuple and the area_triangle object
# to display the answer. note the %.2f operand used to specify how many decimal places for the answer.
if target_shape == shape[1]: # Square
l = float(input("give the length: "))
area_square = l ** 2
print("the area of the %s is %.2f" % (shape[1],area_square))
if target_shape == shape[2]: # Rectangle
l = float(input("give the length: "))
w = float(input("give the width: "))
area_rectangle = l * w
print("the area of the %s is %.2f" % (shape[2],area_rectangle))
if target_shape == shape[3]: # Circle
r = float(input("give the radius: "))
pi = constants # In this line we are calling forth the constants object to use the value of pi.
area_circle = 2 * pi * r
print("the area of the %s is %.2f" %(shape[3],area_circle))
start_up = input("Do you want to calculate another shape's area?: ") # This line allows the user the chance to do just what it says in the brackets.
else: # This is to safegaurd against failure in the event of the user
print("That shape's not in my database") # inputing a shape non-existant in the shape tuple.
start_up = input("Do you want to calculate another shape's area?: ")
if start_up == "no": #This is for when the user no longer wants to use the program.
print("Goodbye then!")
input("Press any key to close")
if start_up != ("yes" or "no"):
is still wrong;('yes' or 'no')
is'yes'
. You're looking fornot in
or 2 inequality tests. \$\endgroup\$