所以我正在挑战,它需要我计算三个不同区域内的行进距离。
1区:0至30km
2区:31至60km
3区:61至90公里
现在假设一辆汽车从1区到2区行驶34公里(进入高速公路24公里,出口58公里),我怎样才能将1区的行驶距离与2区的行驶距离分开。 这就是我到目前为止所做的:
def determine_zone(marker):
""" (int) -> (int)
Return the zone corresponding to the marker (1,2,3) as an int.
>>> determine_zone(34)
2
>>> determine_zone(72)
3
"""
if marker >= ZONE1_BEGIN and marker <= ZONE1_END:
return 1
elif marker >= ZONE2_BEGIN and marker <= ZONE2_END:
return 2
elif marker >= ZONE3_BEGIN and marker <= ZONE3_END:
return 3
distance_travelled = abs(entry - exit)
entry_zone = determine_zone
exit_zone = determine_zone
distance1 = abs(distance_travelled - ZONE2_BEGIN)
print(distance1)
答案 0 :(得分:0)
ZONE1_BEGIN = 0
ZONE1_END = 30
ZONE2_BEGIN = 31
ZONE2_END = 60
ZONE3_BEGIN = 61
ZONE3_END = 90
def determine_zone(marker):
""" (int) -> (int)
Return the zone corresponding to the marker (1,2,3) as an int.
>>> determine_zone(34)
2
>>> determine_zone(72)
3
"""
if marker >= ZONE1_BEGIN and marker <= ZONE1_END:
return 1
elif marker >= ZONE2_BEGIN and marker <= ZONE2_END:
return 2
elif marker >= ZONE3_BEGIN and marker <= ZONE3_END:
return 3
def determine_distance(point,zone,loc = None):
if zone==1:
distance = ZONE1_END - point if loc == 'START' else point - ZONE1_BEGIN +1
elif zone==2:
distance = ZONE2_END - point if loc == 'START' else point - ZONE2_BEGIN +1
elif zone==3:
distance = ZONE3_END - point if loc == 'START' else point - ZONE3_BEGIN +1
return distance
entry = 24
exit = 58
distance_travelled = abs(entry - exit)
print "Distance Travelled : %s"%distance_travelled
entry_zone = determine_zone(24)
exit_zone = determine_zone(58)
## Logic to determin how much distance travelled in each zone
entry_zone_distance = determine_distance(entry,entry_zone,'START')
exit_zone_distance = determine_distance(exit,exit_zone,'END')
print "Car Entered in Zone %s and travelled distance %s"%(entry_zone,entry_zone_distance)
print "Car Exited in Zone %s and travelled distance %s"%(exit_zone,exit_zone_distance)
#distance1 = abs(distance_travelled - ZONE2_BEGIN)
#print(distance1)