SharedClass Samples / calculate-drive-time-with-cmd-line-arguments.pyOpen in CoCalc
Author: Ryan McFall
1import sys
2
3from argparse import ArgumentParser
4
5parser = ArgumentParser()
6parser.add_argument('--distance', type=float, help='The distance to be traveled')
7parser.add_argument('--time-to-save', type=float, required=True, help='The number of minutes you want to save')
8parser.add_argument('--speed-limit', default=70, type=float, help='The speed you normally travel')
9
10arguments = parser.parse_args()
11
12if arguments.distance == None:
13    distance_to_travel = float(raw_input('How far are you going? '))
14else:
15    distance_to_travel = arguments.distance
16
17if 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
25speed_limit = arguments.speed_limit
26time_to_save = arguments.time_to_save
27
28if 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
32numerator = (speed_limit * time_to_save * speed_limit) / (distance_to_travel * 60)
33denominator = 1 - (speed_limit * time_to_save) / (distance_to_travel * 60)
34new_speed = (numerator / denominator) + speed_limit
35
36print 'You should go', new_speed, 'to save', time_to_save, 'minutes over going', speed_limit,
37print 'when traveling', distance_to_travel, 'miles'
38
39
40