I am a relative beginner to Python and as such I've been working on little things here and there in the office that strike me as something interesting that might be fun to try and code a solution.
Recently, I was working with some cell phone data and decided to write a little script that would triangulate a cell phone location from ping strengths/XY locations from 3 different towers. As we only get one cell tower data returns in a exigence request anyway, this was pretty much a practice problem for me, which I would love to check the accuracy on anyway.
As such, I was just looking for some critical feedback on any parts of this that could be written cleaner or functionally more efficient than what i have. It works fine if all the correct values are entered for all user inputs but I am not sure how to keep things moving along if an invalid value is entered. Right now, I just exit out of the script if a invalid number is entered and the user would have to start over. I would like to be able to loop(?) back through with the correct value after a prompt of some sort.
Part 1
User inputs (this was going to be a tool/script to be run from someones desktop in the shell)for tower signal strengths and then standardizing those inputs. I pulled all the math off another thread here.
try:
s1=float (raw_input ("please enter signal strength number 1" + "-->" + " "))
s2=float (raw_input ("please enter signal strength number 2" + "-->" + " "))
s3=float (raw_input (" please enter signal strength number 3" + "-->" + " "))
except (EOFError,ValueError,TypeError):
oops2 = raw_input("An unusuable value was entered for the signal strength, Press ENTER and try again")
print oops2
exit()
if s1 < 0.31 or s1 > 99:
badinput = raw_input("Signal strength #1 is invalid,\
please enter a value between 0.31 - 99. Press ENTER and try again")
print badinput
exit()
if s2 < 0.31 or s2 > 99:
badinput = raw_input("Signal strength #2 is invalid, please enter a value between 0.31 - 99. Press ENTER and try again")
print badinput
exit()
if s3 < 0.31 or s3 > 99:
badinput = raw_input("Signal strength #3 is invalid, please enter a value between 0.31 - 99. Press ENTER and try again")
print badinput
exit()
print "_______________"
print ""
else:
def signal_strength(s1,s2,s3):
w1=float(s1)/(s1+s2+s3)
return w1
tower1_strength=signal_strength(s1,s2,s3)
print ("standardized signal strength for tower #1-->") + (" ") + str(tower1_strength)
def signal_strength2(s1,s2,s3):
w2=float(s2)/(s1+s2+s3)
return w2
tower2_strength=signal_strength2(s1,s2,s3)
print ("standardized signal strength for tower #2-->") + (" ") + str(tower2_strength)
def signal_strength3(s1,s2,s3):
w3=float(s3)/(s1+s2+s3)
return w3
tower3_strength=signal_strength3(s1,s2,s3)
print ("standardized signal strength for tower #3-->") + (" ") + str(tower3_strength)
print "_______________"
print ""
Part II
User inputs to select which three towers to choose the XY data for, which is held in a dictionary. Right now I just have three sample values in the dictionary rather than all the cell tower xy's located in my AOI.
# sample dictionary for cell carrier tower ID:X,Y Coordinates (Decimal Degrees), will use carrier ID's as key
longitude = {1:-89.928573,2:-89.813409,3:-89.825694}
latitude = {1:43.899751,2:43.913357,3:43.82814}
try:
ID1 = int(raw_input ("Carrier ID for tower #1" + "-->" + " "))
ID1x= longitude[ID1]
ID1y = latitude[ID1]
ID2 = int(raw_input ("Carrier ID for tower #2" + "-->" + " "))
ID2x = longitude[ID2]
ID2y = latitude[ID2]
ID3 = int(raw_input ("Carrier ID for tower #3" + "-->" + " "))
ID3x = longitude[ID3]
ID3y = latitude[ID3]
except Exception,e:
oops = raw_input ("An invalid Tower ID# was used,Please use the Carrier/Provider supplied identification number. Press ENTER and try again")
print oops
exit()
#Need to figure out how to loop back through the Tower ID# (and signal strength for that matter) data entry chunk after exception, if possible
print "_______________"
print ""
def user_x(tower1_strength,tower2_strength,tower3_strength,tx1,tx2,tx3):
x_location=float(tower1_strength*tx1)+(tower2_strength*tx2)+(tower3_strength*tx3)
return x_location
X_Coord=user_x(tower1_strength,tower2_strength,tower3_strength,ID1x,ID2x,ID3x)
print ("the X coordinate for the position is-->") + (" ") + str(X_Coord)
def user_y(tower1_strength,tower2_strength,tower3_strength,ty1,ty2,ty3):
y_location=float(tower1_strength*ty1)+(tower2_strength*ty2)+(tower3_strength*ty3)
return y_location
Y_Coord=user_y(tower1_strength,tower2_strength,tower3_strength,ID1y,ID2y,ID3y)
print ("the Y coordinate for the position is-->") + (" ") + str(Y_Coord)
print "_______________"
print ""
ty = raw_input ("thank you")
print ty