Hi,
I'm newbie in Python Programming. I created the below code. I want to optimize it, but dont know where to start. I am sure, I can do the same with less lines of code. Please advise on best practices and ways on how to optimize it.
import math
def side_1():
print ''
print "**********************************"
print " Pythagorean Calculator "
print "**********************************"
print ''
a = raw_input("Please enter the value of A: ")
b = raw_input("Please enter the value of B: ")
if a.isalpha():
print "Sorry, that is not a valid option."
else:
a = float(a)
b = float(b)
c = math.sqrt((a**2) + (b**2))
print "The value for A: " + "%.2f" % round(a,2)
print "The value for B: " + "%.2f" % round(b,2)
print "The value for C: " + "%.2f" % round(c,3)
print ''
flag = raw_input('Would you like to run the program again? [Y/N] ').upper()
if flag == 'Y' and flag.isalpha():
print ''
print("Let's proceed")
side_1()
else:
print('I will quit')
print('')
def side_2():
print ''
print "**********************************"
print " Pythagorean Calculator "
print "**********************************"
print ''
b = raw_input("Please enter the value of B: ")
c = raw_input("Please enter the value of C: ")
if b.isalpha():
print "Sorry, that is not a valid option."
else:
b = float(b)
c = float(c)
a = math.sqrt((b**2) - (c**2))
print "The value for B: " + "%.2f" % round(b,2)
print "The value for C: " + "%.2f" % round(c,2)
print "The value for A: " + "%.2f" % round(a,3)
print ''
flag = raw_input('Would you like to run the program again? [Y/N] ').upper()
if flag == 'Y' and flag.isalpha():
print ''
print("Let's proceed")
side_2()
else:
print('I will quit')
print('')
def side_3():
print ''
print "**********************************"
print " Pythagorean Calculator "
print "**********************************"
print ''
c = raw_input("Please enter the value of C: ")
a = raw_input("Please enter the value of A: ")
if c.isalpha():
print "Sorry, that is not a valid option."
else:
c = float(c)
a = float(a)
b = math.sqrt((c**2)- (a**2))
print "The value for A: " + "%.2f" % round(a,2)
print "The value for C: " + "%.2f" % round(c,2)
print "The value for B: " + "%.2f" % round(b,3)
print ''
flag = raw_input('Would you like to run the program again? [Y/N] ').upper()
if flag == 'Y' and flag.isalpha():
print ''
print("Let's proceed")
side_3()
else:
print('I will quit')
print('')
def main():
print ''
print "**********************************"
print " Pythagorean Main Menu "
print "**********************************"
print" 1. I have side A and B"
print" 2. I have side B and C"
print" 3. I have side C and A"
option = raw_input('Please select an option:')
if option == '1':
side_1()
elif option == '2':
side_2()
elif option == '3':
side_3()
else:
print "Not a Valid option"
main()
main()