SharedClass Samples / calculate-drive-time-with-cmd-line-arguments.pyOpen in CoCalc
Author: Ryan McFall
1
import sys
2
3
from argparse import ArgumentParser
4
5
parser = ArgumentParser()
6
parser.add_argument('--distance', type=float, help='The distance to be traveled')
7
parser.add_argument('--time-to-save', type=float, required=True, help='The number of minutes you want to save')
8
parser.add_argument('--speed-limit', default=70, type=float, help='The speed you normally travel')
9
10
arguments = parser.parse_args()
11
12
if arguments.distance == None:
13
distance_to_travel = float(raw_input('How far are you going? '))
14
else:
15
distance_to_travel = arguments.distance
16
17
if distance_to_travel <= 0:
18
print 'The distance to travel should be positive'
19
sys.exit ()
20
21
#distance_to_travel = float(raw_input('How far are you going? '))
22
#speed_limit = float(raw_input('What is the speed limit? '))
23
#minutes_to_save = float(raw_input('How many minutes do you want to save? '))
24
25
speed_limit = arguments.speed_limit
26
time_to_save = arguments.time_to_save
27
28
if time_to_save > distance_to_travel / speed_limit * 60:
29
print 'The value for time to save must be less than the time it takes normally'
30
sys.exit()
31
32
numerator = (speed_limit * time_to_save * speed_limit) / (distance_to_travel * 60)
33
denominator = 1 - (speed_limit * time_to_save) / (distance_to_travel * 60)
34
new_speed = (numerator / denominator) + speed_limit
35
36
print 'You should go', new_speed, 'to save', time_to_save, 'minutes over going', speed_limit,
37
print 'when traveling', distance_to_travel, 'miles'
38
39
40