CoCalc Public FilesClass Samples / calculate-drive-time-with-cmd-line-options.pyOpen with one click!
Author: Ryan McFall
Compute Environment: Ubuntu 20.04 (Default)
1
from argparse import ArgumentParser
2
3
parser = ArgumentParser()
4
5
distance_to_travel = 0
6
7
parser.add_argument('--distance', dest='distance_to_travel', type=float, help='The distance you will travel')
8
9
parser.add_argument('--speed-limit', type=int, help='The speed limit on the road', default=70)
10
11
arguments=parser.parse_args()
12
13
if not arguments.distance_to_travel:
14
distance_to_travel = float(input('How far are you going? '))
15
else:
16
distance_to_travel = arguments.distance_to_travel
17
18
if not arguments.speed_limit:
19
speed_limit = float(input('What is the speed limit? '))
20
else:
21
speed_limit = arguments.speed_limit
22
23
minutes_to_save = float(input('How many minutes do you want to save? '))
24
25
numerator = (speed_limit * minutes_to_save * speed_limit) / (distance_to_travel * 60)
26
denominator = 1 - (speed_limit * minutes_to_save) / (distance_to_travel * 60)
27
28
new_speed = (numerator / denominator) + speed_limit
29
30
print ('You should go', new_speed, 'to save', minutes_to_save, 'minutes over going', speed_limit, end=' ')
31
print ('when traveling', distance_to_travel, 'miles')
32