Been lurking in the background quite a while, I mainly prefer reading others posts over asking questions which have been answered many times before.

However, I have only recently started learning about programming in Python, and need some tips on tidying up the code i have written. i.e. removing whitespace, minimizing unnecessary code etc.

The following is an area, perimeter and circumference calculator i wrote at work today, basically my first program in python (have done similar in VB and even [during my short attempt at learning the language] C++).

A lot of the blank lines used are for my own sake, they make it easier for me to read and debug, however even I can recognise that what i have written is very long and could be done using much less code, time and effort.

Would it be possible to get some feedback from some of the more experienced programmers on here on programming techniques i should learn, or just general tips, to write shorter and more efficient code?

Thanks in advance,

Code below:

- Code: Select all
`while True:`

option1 = input("\n\nWelcome to the Area and Perimeter Calculator, would you like to continue (y/n)?: ")

if option1 == "y":

option3 = input("Are you looking to find the Area and Perimeter / Circumference of a rectangle, circle or triangle? \n For a rectangle enter 'r', \n For a circle enter 'c', \n For a triangle enter 't', \n Please make your choice: ")

if option3 == "r":

l = float(input("\n Please input the length of the object: "))

w = float(input("\n Please input the width of the object: "))

area = float(l*w)

perimeter = float((l*2)+(w*2))

while True:

option2 = input("\nWould you like to calculate Area 'a', Perimeter 'p', or Both 'b'?: ")

if(option2 == "a"):

print("\n The area is ", area,", \n thank you for using.")

break

elif(option2 == "p"):

print("\n The perimeter is ", perimeter,", \n thank you for using.")

break

elif(option2 == "b"):

print("\n The area is: ", area, "\n The perimeter is: ", perimeter, "\n Thank you for using!.")

break

else:

print("Please insert either a, p, or b to determine which answer you require.")

elif option3 == "c":

r = float(input("\n Please input the radius of the circle: "))

area = float(3.141592 * (r**2))

circ = float(2 * 3.141592 * r)

while True:

option2 = input("\nWould you like to calculate Area 'a', Circumference 'c', or Both 'b'?: ")

if(option2 == "a"):

print("\n The area is ", area,", \n thank you for using.")

break

elif(option2 == "c"):

print("\n The circumference is ", circ,", \n thank you for using.")

break

elif(option2 == "b"):

print("\n The area is: ", area, "\n The perimeter is: ", circ, "\n Thank you for using!.")

break

else:

print("Please insert either a, p, or b to determine which answer you require.")

elif option3 == "t":

print("\n This section is currently under construction!")

elif option1 == "n":

print("\n That's ok")

break

else:

print("please answer either yes or no.")

print("\n All Calculations completed. \n Thank you for using!")