In: Computer Science
def DMStoDD(degree, minutes, seconds): dd = abs(degree) + abs(minutes)/60 + abs(seconds)/3600 if degree > 0: return dd else: return -dd print(DMStoDD(-30, 30, 00))
In this script, add a main program that collects DD or DMS input from the user, then converts it to the other form and reports the converted value. Directions: The main program needs to gather a string from the keyboard using input() and detect whether it is DD or DMS (Hint: use the split function). Then it must pass the proper DD or DMS values to the functions above to convert. The DMS input is separated by comma and contains no space. The degree field could be negative to represent a negative value. (E.g. 43,4,23 or -43,4,23)
If you have any doubts, please give me comment...
def DMStoDD(degree, minutes, seconds):
dd = abs(degree) + abs(minutes)/60 + abs(seconds)/3600
if degree > 0:
return dd
else:
return -dd
def DDtoDMS(dd):
degree = int(abs(dd))
minutes = int((abs(dd)-degree)*60)
seconds = (abs(dd) - degree - minutes/60) * 3600
if(dd<0):
degree = -degree
return "%d,%d,%d"%(degree, minutes, seconds)
inp = input("Enter input: ")
d = inp.split(",")
if len(d)==3:
print(DMStoDD(int(d[0]), int(d[1]), int(d[2])))
else:
print(DDtoDMS(float(d[0])))