The following is the (semi) finished version of a simple Mils to Degrees conversion calculator I built in Python 2.7 as a learning exercise. I am new to Python and still working out the kinks. The premise is, the user chooses to convert mils to degrees or vice versa. The user enters an azimuth of the chosen unit and it is converted to the other. If the user chooses an invalid menu option he is notified as such. If he enters an azimuth outside of the range (1-360 for degrees, 1-6400 for mils) he is notified and must start over. The problem is that if the user enters an invalid azimuth, say 365 degrees, he is kicked back to the menu. Is there a way to return to the previous step in the if/else loop? I've searched the forums and documentation and it doesn't appear to be possible. Also, since I'm new to Python, how can make this code more efficient? Do I need to have the if and elif statements in the function definitions or can I combine them without creating errors or redundant output? I've not been successful in doing so. All input is welcome and appreciated.
#Mil-Deg.py
#Simple Mils / Degrees Conversion Calculator
#6 JAN 2013
#Note: 6400 mils / 360 degrees in a circle. 17.78 mils to one degree.
import sys
import math
import winsound
#Define menu function
def menu():
print
print " Mils / Degrees Conversion Calculator"
print "-" * 38
print
print "Options: "
print "1. Degrees to Mils"
print
print "2. Mils to Degrees"
print
print "3. Quit"
print "-" * 20
print
return input ("Choose your option: ")
print
#Define mils to degrees function
def m2d(a):
if a <= 6400 and a >= 1: #get user input within given range
b = 17.78
c = round((a / b),0) #convert and round to nearest degree
if c > 359 or c < 1: #change 0 degrees to 360
c = 360
#Output
print
print a, "mils =", int (c), "degrees"
else:
print
print a, "mils =", int (c), "degrees"
elif a > -1 and a < 1: #change 0 mils to 6400
a = 6400
b = 17.78
c = round((a / b), 0) #math, same as above
if c > 359 or c < 1: #0 to 360, same as above
c = 360
#Output
print
print 6400, "mils =", int (c), "degrees"
else:
print
print a, "mils =", int (c), "degrees"
else:
#warning, mulligan
winsound.Beep(440, 500)
print
print "*** There are only 6400 mils in a circle. Try again. ***"
print
print "-" * 38
print
#define degrees to mils function
def d2m(b):
if b <= 360 and b > 0: #get user input within given range
a = 17.78
c = round((b * a),0) #convert and round to nearest mil
if c >= 6400: #set limit to 6400 mils
c = 6400
#Output
print
print b, "degrees =", int (c), "mils"
else:
print
print b, "degrees =", int (c), "mils"
elif b > -1 and b < 1: #change 0 to 360 degrees
b = 360
a = 17.78
c = round((b * a),0) #math, same as above
if c >= 6400:
c = 6400
#Output
print
print 360, "degrees =", int (c), "mils"
else:
print
print b, "degrees =", int (c), "mils"
else:
#warning
winsound.Beep(440, 500)
print
print "*** There are only 360 degrees in a circle. Try again. ***"
print
print "-" * 38
print
#Begin program
loop = 1
choice = 0
while loop == 1:
choice = menu() #Menu function call
if choice == 1: #If user chooses degrees to mils:
#Output
print
print "Enter your azimuth in degrees (1 - 360)"
print
d2m(input("Degrees: ")) #function call
elif choice == 2: #If user chooses mils to degrees:
#Output
print
print "Enter your azimuth in mils (1 - 6400)"
print
m2d(input("Mils: ")) #function call
elif choice == 3: #If user chooses quit, exit program
loop = 0
elif choice != 1 and choice != 2 and choice != 3: #if user makes invalid menu choice:
#warning
winsound.Beep(440, 500)
print
print
print
print choice, "is not a valid choice."
print
print "Please choose from the available options."
print
print
print
print "-" * 38
print
print "Thank you for using Mil-Deg.py"
print
print